Grep并用特殊字符替换文本

时间:2019-05-23 13:39:55

标签: linux replace grep

我有10K + XML,其中大约一半具有我要替换的以下代码行:

<protocol_name_from_source><![CDATA[This section will be completed when reviewed by an Expert Review Panel.]]></protocol_name_from_source>

与此:

<protocol_name_from_source><![CDATA[Not applicable.]]></protocol_name_from_source>

我已经成功grep处理了受影响的文件: grep -rl '<process\_review><\!\[CDATA\[<p>The Expert Review Panel has not reviewed this measure yet\.<\/p>\]\]><\/process\_review>' ./

但是我似乎无法用sed替换文本:

grep -rl '<process\_review><\!\[CDATA\[<p>The Expert Review Panel has not reviewed this measure yet\.<\/p>\]\]><\/process\_review>' ./ | xargs sed -i 's/<process\_review><\!\[CDATA\[<p>The Expert Review Panel has not reviewed this measure yet\.<\/p>\]\]><\/process\_review>/<process\_review><\!\[CDATA\[<p>Not applicable\.<\/p>\]\]><\/process\_review>/g'

提前感谢您的帮助。

edit:这些XML在git repo中。有损坏回购协议的风险吗?

1 个答案:

答案 0 :(得分:0)

嗯,根据我的手册页,sed -i选项后面应加上一个(最终为0长度)扩展名。如果命令选项不是唯一的命令参数,则应在命令选项中加上-e

所以我在这里用:

grep -rl '<process\_review><\!\[CDATA\[<p>The Expert Review Panel has not reviewed this measure yet\.<\/p>\]\]><\/process\_review>' ./ | xargs sed -i '' -e 's/<process\_review><\!\[CDATA\[<p>The Expert Review Panel has not reviewed this measure yet\.<\/p>\]\]><\/process\_review>/<process\_review><\!\[CDATA\[<p>Not applicable\.<\/p>\]\]><\/process\_review>/g'

当心,我还没有审查(long ...)sed s/.../.../命令...