我的文件内容如下,所需的输出如下所示。使用单独的 sed 命令,我可以修改文件内容。说
sed -i -e 's!<tag1>FIELD1</tag1>!<tag1>Replaced contents of field1</tag1>! filename
但是我很难用单个 sed 脚本文件替换这些单独的命令。
This is a sample file containing a few tags
<tag1>FIELD1</tag1>
<tag2>FIELD2</tag2>
<tag1>FIELD1 Do not change me</tag1>
<tag2>FIELD2 Do not change me</tag2>
<tag1>FIELD1 Do not change me</tag1>
<tag2>FIELD2 Do not change me</tag2>
所需的输出是
This is a sample file containing a few tags
<tag1>Replaced contents of field1</tag1>
<tag2>Replaced contents of field2</tag2>
<tag2>Some addition to field2</tag2>
<tag1>FIELD1 Do not change me</tag1>
<tag2>FIELD2 Do not change me</tag2>
<tag1>FIELD1 Do not change me</tag1>
<tag2>FIELD2 Do not change me</tag2>