用sed替换包含特殊字符的字符串
我浏览了这里和其他地方关于这个主题的许多帖子,但所提供的解决方案似乎都不适合我。我需要在特定文件夹内的所有文件中用另一个字符串(包含正确的 PHP 代码)替换一个字符串(其中包含有错误的无意义 PHP 代码)。据我所知,使用 sed 时唯一需要转义的字符是与号、反斜杠以及用作分隔符的字符。
我想要搜索的字符串是:
$arguments = func_get_args();$numargs = func_num_args();for($i=1; $i < $numargs; $i++){$arguments[$i] = $arguments = func_get_args();$arguments[$i];}
我想要替换它的字符串是:
$arguments = func_get_args();$numargs = func_num_args();for($i=1; $i < $numargs; $i++){$arguments[$i] = &$arguments[$i];}
我尝试了多种变体,但无法让它工作。我认为以下命令应该起作用(但不起作用)。
find /folder/to/search -name Function.php -type f -exec sed -i 's|$arguments = func_get_args();$numargs = func_num_args();for($i=1; $i < $numargs; $i++){$arguments[$i] = $arguments = func_get_args();$arguments[$i];}|$arguments = func_get_args();$numargs = func_num_args();for($i=1; $i < $numargs; $i++){$arguments[$i] = \&$arguments[$i];}|' {} \;
这让我发疯 - 任何帮助将不胜感激!
I have looked through many posts here and elsewhere on this subject, but none of the solutions offered seem to work for me. I need to replace a string (which contains buggy nonsensical PHP code) with another string (containing the correct PHP code) in all files within a particular folder. I understand that the only characters that need escaping when using sed are ampersands, backslashes, and whichever character is used as the delimiter.
The string I want to search for is:
$arguments = func_get_args();$numargs = func_num_args();for($i=1; $i < $numargs; $i++){$arguments[$i] = $arguments = func_get_args();$arguments[$i];}
The string I want to replace it with is:
$arguments = func_get_args();$numargs = func_num_args();for($i=1; $i < $numargs; $i++){$arguments[$i] = &$arguments[$i];}
I have tried numerous variations but cannot get it to work. I think the following command should work (but doesn't).
find /folder/to/search -name Function.php -type f -exec sed -i 's|$arguments = func_get_args();$numargs = func_num_args();for($i=1; $i < $numargs; $i++){$arguments[$i] = $arguments = func_get_args();$arguments[$i];}|$arguments = func_get_args();$numargs = func_num_args();for($i=1; $i < $numargs; $i++){$arguments[$i] = \&$arguments[$i];}|' {} \;
This is driving me insane - any assitance would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我终于找到了正确的语法。与我读过的所有建议相反,我需要在两个表达式中转义美元符号和左方括号(但不是右方括号或弯括号)。所以这是最终对我有用的命令:
再次感谢那些回复的人。
I finally found the right syntax. Contrary to all the advice I have read, I needed to escape the dollar signs and open square brackets in BOTH expressions (but not the close square brackets nor the curved brackets). So here is the command that finally worked ok for me:
Thanks again to those who replied.
对于搜索表达式来说 Not true。它是一个正则表达式;对于文字匹配,您需要转义所有正则表达式特殊符号,即至少
s%\([][^$.\*]\)%\\\1%g
(即兴 - 我可能错过了一些,和/或你的 sed 可能与我的不同)。幸运的是,这很容易通过编程来完成,例如使用 sed - 只是要小心,以免混淆。替换字符串是文字字符串,反向引用除外,因此对于该部分,您的陈述是正确的。
Not true for the search expression. It is a regular expression; for literal matching, you need to escape all regex specials, i.e. at least
s%\([][^$.\*]\)%\\\1%g
(off the cuff - I might have missed a few, and/or your sed may differ from mine). Luckily, this is easy to do programmatically, e.g. with sed - just be careful so you don't get confused.The replacement string is a literal string, except for backreferences, so for that part your statement is true.