简体   繁体   English

Grep并用特殊字符替换文本

[英]Grep and replace text with special characters

I have 10K+ XML where about half of them have the following line of code I'd like to replace: 我有10K + XML,其中大约一半有我想要替换的以下代码行:

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

with this: 有了这个:

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

I've been able to successfully grep for the affected files: grep -rl '<process\\_review><\\!\\[CDATA\\[<p>The Expert Review Panel has not reviewed this measure yet\\.<\\/p>\\]\\]><\\/process\\_review>' ./ 我已经能够成功地grep受影响的文件: grep -rl '<process\\_review><\\!\\[CDATA\\[<p>The Expert Review Panel has not reviewed this measure yet\\.<\\/p>\\]\\]><\\/process\\_review>' ./

but I can't seem to be able to replace the text with sed: 但我似乎无法用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'

Appreciate any help in advance. 提前感谢任何帮助。

edit: These XMLs are in a git repo. 编辑:这些XML在git仓库中。 Is there any risk of corrupting the repo? 有破坏回购的风险吗?

Hmm, according to my man page, sed -i option expect to be followed with one (eventually 0 length) extension. 嗯,根据我的手册页,sed -i选项期望跟随一个(最终为0长度)扩展。 And the command option should be introduced with -e if it is not the only command parameter. 如果命令选项不是唯一的命令参数,则应使用-e引入命令选项。

So here I would use: 所以在这里我会用:

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'

Beware, I have not reviewed the (long...) sed s/.../.../ command... 小心,我没有评论(长...)sed s/.../.../命令...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM