gpt4 book ai didi

html - 使用 sed 替换 HTML 标签内容

转载 作者:太空狗 更新时间:2023-10-29 13:46:48 26 4
gpt4 key购买 nike

我正在尝试在 bash 脚本中使用 sed 替换 HTML 页面中某些 HTML 标记的内容。出于某种原因,我没有得到正确的结果,因为它没有替换任何东西。它必须是非常简单/愚蠢的我忽略的东西,有人愿意帮助我吗?

要搜索/替换的 HTML:

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 页面并根据一些外部数据更新图形。第一次运行时,标签的内容将为空,之后将被填充。


编辑:

我最终使用了导致以下代码的答案的组合:

sed -i -e 's|<span id="unlockedCount">\([0-9]\{0,\}\)</span>|<span id="unlockedCount">'"${unlockedCount}"'</span>|g' index.html

非常感谢@Sorpigal、@tripleee、@classic 的帮助!

最佳答案

试试这个:

sed -i -e "s/\(<span id=\"unlockedCount\">\)\(<\/span>\)/\1${unlockedCount}\2/g" index.html

关于html - 使用 sed 替换 HTML 标签内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7189604/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com