更换使用SED HTML标签内容 [英] Replacing HTML tag content using sed

查看:182
本文介绍了更换使用SED HTML标签内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图来代替某些HTML标记在HTML页面中bash脚本使用SED的内容。出于某种原因,我没有得到正确的结果,因为它不是取代任何东西。它必须很简单的东西/愚蠢IM远眺,任何人都关心帮助我吗?

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的命令:

sed command used:

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.

编辑:

我最终使用导致在以下code答案的组合:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆