Reputation: 23062
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(
.
In the past, I have successfully used the following grep & sed statement:
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?
Upvotes: 0
Views: 231
Reputation: 37767
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.
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).
So sed actually sees \(
(not just (
), and that has a special meaning in its regular expressions.
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.
Upvotes: 4