简体   繁体   English

sed:如何使此bash命令更好地工作?

[英]sed: How can I make this bash command work/work better?

My objective is to simply to change a function call from one to another, anywhere the first one is found, recursively. 我的目标是简单地将找到的第一个函数调用递归地更改为另一个函数调用。 In this case, mysql_error( to mysql_error_to_log( . 在这种情况下, mysql_error(mysql_error_to_log(

In the past, I have successfully used the following grep & sed statement: 过去,我已成功使用以下grep&sed语句:

grep -rl connection1\.php ./ | xargs sed -i
    's/connection1\.php/account_db_user\.connection\.php/'

So this time I tried something similar (just a different character, ( , being escaped): 因此,这次我尝试了类似的操作(只是一个不同的字符( ,被转义了):

grep -rl mysql_error\( ./ | xargs sed -i 's/mysql_error\(/mysql_error_to_log\(/'

but I receive this error: 但我收到此错误:

sed: -e expression #1, char 37: Unmatched ( or \(

It seems to be telling me that my attempts to escape ( aren't correct. I searched for an explanation for that sed issue on google, but didn't find anything really clear. So what's wrong with that command? 似乎是在告诉我,我尝试进行转义(是不正确的。我在google上搜索了有关sed问题的解释,但没有发现真正清楚的东西。那么该命令出了什么问题?

You do not need to escape ( or ) in quoted strings on the shell, whether those strings are quoted with single '()' or double "()" quotes. 无论这些字符串是用单引号'()'还是双引号"()"引起来的,您都不需要在外壳上的引号字符串中转义()

The only reason the \\( in the argument to grep works is because that string is not quoted. grep参数中\\(起作用的唯一原因是因为该字符串未加引号。

Remember that it is the shell which deals with the quotes (and with the \\( of the unquoted strings), not the programs the shell actually calls (grep and xarg, in your case). 请记住,是外壳处理引号(以及未引用字符串的\\( ),而不是外壳实际调用的程序(在您的情况下为grep和xarg)。

So sed actually sees \\( (not just ( ), and that has a special meaning in its regular expressions. 所以sed实际上看到了\\( (不只是( ),并且在正则表达式中有特殊含义。

My general advice would be to always quote strings on the shell whenever there might be a "funny" character in them, and thus avoid escaping characters from the shell altogether. 我的一般建议是,只要它们中可能包含“有趣”的字符,就总是在壳上加引号,从而避免将字符完全从壳中转义。

As to the "old" example with the escaped dot \\. 对于带有转义点\\.的“旧”示例\\. - this appears to work, but one time you are escaping the dot from the shell (which would not be necessary but results in just a verbatim dot . ), and the other time you escape the dot in a regular expression, where an unescaped dot has a special meaning, thus requiring the escaping to mean a verbatim dot. -这似乎可行,但是有一次您是从外壳转义点(这不是必需的,只会产生一个逐字的点. ),而另一次您是在正则表达式中转义该点,而未转义的点具有特殊含义,因此要求转义表示逐字点。

尝试时不要任何逃脱(对我有用)。

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

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