我需要使用Sed进行搜索和替换。我正在为/**#
取代define('WP_POST_REVISIONS', 3);\n\n/**#
。
但我无法弄清楚正确的逃避。即使在逃避(明显需要的)单引号后,我仍然得到一个bash: syntax error near unexpected token ')'
在这种情况下,适当的逃避是什么?
试着替换你的:
find /start/path -name *.html -exec sed -ie 's|/**#|define(\'WP_POST_REVISIONS\', 3);|g' '{}' \;
有:
find /start/path -name '*.html' -print0 \
| xargs -0 -n 1 sed -ie 's|/\*\*#|define('\''WP_POST_REVISIONS'\'', 3);\n/\*\*#|g'
告诉我们它给你的东西
(我试图猜测你在文件中寻找实际的字符串“/ **#”...请给我们一些你真正想要的例子,如果它不是那个实际的字符串)
它不是sed逃避,而是bash逃避。
转义在单引号内不起作用('
)
你可以使用双引号("
),如果你没有像参数中的“$ \”这样的特殊字符(或者在必要时将其转义):
find /start/path -name *.html -exec sed -ie "s/abc/define('WP_POST_REVISIONS', 3);/g" '{}' \;
或者引用$'
,它支持转义:
find /start/path -name *.html -exec sed -ie $'s/abc/define(\'WP_POST_REVISIONS\', 3);/g' '{}' \;