简体   繁体   English

逃离SED中的正则表达式

[英]Escaping Regex in SED

I need to use Sed to do a search and replace. 我需要使用Sed进行搜索和替换。 I'm replacing /**# for define('WP_POST_REVISIONS', 3);\\n\\n/**# . 我正在替换/**# 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 ')' 即使在转义(显然需要的)单引号后,我仍然会遇到bash: syntax error near unexpected token ')'

What is the proper escaping in this case? 在这种情况下,适当的逃避是什么?

It is not sed escaping, but bash escaping. 它不是sed逃避,而是bash逃避。

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) (我试图猜测你在文件中寻找实际的字符串“/ **#”...请给我们一些你真正想要的例子,如果它不是那个实际的字符串)

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

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