简体   繁体   中英

Escaping Regex in SED

I need to use Sed to do a search and replace. I'm replacing /**# for define('WP_POST_REVISIONS', 3);\\n\\n/**# .

But I can't figure out the proper escaping. Even after escaping the (obviously needed) single quotes, I still get a bash: syntax error near unexpected token ')'

What is the proper escaping in this case?

It is not sed escaping, but bash escaping.

Escaping does not work within single-quotes ( ' )

You can use double-quotes ( " ), if you have no special characters like "$\\ in the parameter (or escape them there if necessary):

find /start/path -name *.html -exec sed -ie "s/abc/define('WP_POST_REVISIONS', 3);/g" '{}' \;

Or quote using $' , which supports escaping:

find /start/path -name *.html -exec sed -ie $'s/abc/define(\'WP_POST_REVISIONS\', 3);/g' '{}' \;

try to replace your:

find /start/path -name *.html -exec sed -ie 's|/**#|define(\'WP_POST_REVISIONS\', 3);|g' '{}' \;

with:

find /start/path -name '*.html' -print0  \
  | xargs -0 -n 1 sed -ie 's|/\*\*#|define('\''WP_POST_REVISIONS'\'', 3);\n/\*\*#|g'

and tell us what it gives you

(I tried to guess you were looking for the actual string "/**#" in your file(s) ... please give us examples of what you are really looking for, if it isn't that actual string)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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