[英]Replace all instances of a character inside a specific HTML element/tag
Adobe Dreamweaver's Search & Replace feature offers to limit its scope to "only inside of the xxxx HTML tag". Adobe Dreamweaver 的搜索和替换功能将其 scope 限制为“仅在 xxxx HTML 标记内部”。
I want to do this with Mac OS X' command line (so will do anything that comes bundled with it).我想用 Mac OS X 的命令行来做这件事(所以会做任何与它捆绑在一起的事情)。
For example, how do I remove all instances of the character "a" inside all <h1>
with the command line?例如,如何使用命令行删除所有
<h1>
中字符“a”的所有实例?
You can use unix's sed command (which is available on mac too).您可以使用 unix 的 sed 命令(也可以在 mac 上使用)。 eg
例如
$ cat foo.xml
<h1>axyzabca</h1>
<a href="foo.com">abc</a>
<h1>aa</h1>
<h1>a</h1>
<h1></h1>
$ cat foo.xml | sed 's/<h1>a*\([^a]*\)a*\([^a]*\)a*<\/h1>/<h1>\1\2<\/h1>/g'
<h1>xyzbc</h1>
<a href="foo.com">abc</a>
<h1></h1>
<h1></h1>
<h1></h1>
(foo.xml is a sample input which covers common test cases) (foo.xml 是一个涵盖常见测试用例的示例输入)
This isn't much of a one-liner, but --这不是一个单行,但是——
perl -ni -e '
$/ = undef; $x = <>; $y = "";
while ($x =~ m#^(.*?<h1>)(.*?)(</h1>)(.*)$#si) {
$x = $4; $y .= $1; $c = $3;
($b = $2) =~ s/a/(something else)/g;
$y .= $b . $c;
} print $y . $x;
' filename.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.