使用 sed 替换 HTML 标签内容 [英] Replacing HTML tag content using sed
问题描述
我正在尝试在 bash 脚本中使用 sed 替换 HTML 页面中某些 HTML 标记的内容.出于某种原因,我没有得到正确的结果,因为它没有取代任何东西.它必须是我忽略的非常简单/愚蠢的事情,有人愿意帮助我吗?
I'm trying to replace the content of some HTML tags in an HTML page using sed in a bash script. For some reason I'm not getting the proper result as it's not replacing anything. It has to be something very simple/stupid im overlooking, anyone care to help me out?
要搜索/替换的 HTML:
HTML to search/replace in:
Unlocked <span id="unlockedCount"></span>/<span id="totalCount"></span> achievements for <span id="totalPoints"></span> points.
使用的sed命令:
cat index.html | sed -i -e "s/<span id="unlockedCount">([0-9]{0,})</span>/${unlockedCount}/g" index.html
这样做的目的是解析HTML页面,并根据一些外部数据更新图形.第一次运行时,标签的内容将为空,之后它们将被填充.
The point of this is to parse the HTML page and update the figures according to some external data. For a first run, the contents of the tags will be empty, after that they will be filled.
我最终使用了导致以下代码的答案组合:
I ended up using a combination of the answers which resulted in the following code:
sed -i -e 's|<span id="unlockedCount">([0-9]{0,})</span>|<span id="unlockedCount">'"${unlockedCount}"'</span>|g' index.html
非常感谢@Sorpigal、@tripleee、@classic 的帮助!
Many thanks to @Sorpigal, @tripleee, @classic for the help!
推荐答案
试试这个:
sed -i -e "s/(<span id="unlockedCount">)(</span>)/1${unlockedCount}2/g" index.html
这篇关于使用 sed 替换 HTML 标签内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!