2

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?

1
  • Sounded link you wanted to stop using regex in sed. You know... escape it? Commented Jun 6, 2013 at 15:23

2 Answers 2

2

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)

Sign up to request clarification or add additional context in comments.

5 Comments

this is what I actually need, without any escaping, for clarity sed -ie "s|/**#|define('WP_POST_REVISIONS', 3);\n/**#|g"
@Gaia: your /**# regexp is probably invalid: what kind of string do you need to match: "////#" or "/#" or "#" ? (ie: /*#) , or "/.......#" (...=anything) ? (ie: /.*#)
I assumed you were trying to match the actual string : /**# so I replaced your regexp with the proper thing : /\*\*# (in sed, an un-escaped * means "the preceding character 0, 1 or more times")
@Gaia: Welcome to stackoverflow. Please consider updating your question, rather than maintaining multiple conversations (essentially about the same issue) that any new readers will have to read and digest. Good luck.
@Gaia: you may want to read about regular expressions (for example your colleagues or local library may have this book : shop.oreilly.com/product/9780596528126.do)
2

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' '{}' \;

4 Comments

How do I add a CRLF with double quotes?
this is what I actually need, without any escaping, for clarity sed -ie "s|/**#|define('WP_POST_REVISIONS', 3);\n/**#|g"
Type the quote, press enter, if you need a line break. And use /[*][*] to match /**
Thank you, but Olivier nailed it first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.