如何将特殊字符串传递给sed [英] How to pass special character string to sed

查看:71
本文介绍了如何将特殊字符串传递给sed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将特殊字符串传递给sed命令,但没有成功,我尝试了反斜杠但没有成功

Am trying to pass special character string to sed command but no success i tried backslash but no success

sed -i 's/old/new/g' {} +   # working 

sed -i 's/$_REQUEST['old']/$_REQUEST['new']/g' {} +   # not working 
sed -i 's/$_REQUEST[\'old\']/$_REQUEST[\'new\']/g' {} +   # not working 
sed -i "s/$_REQUEST['old']/$_REQUEST['new']/g" {} +  # NIGHTMARE ! not working 

推荐答案

即使在转义的字符串中也不能包含单引号.

It's not possible to include single quotes inside a single quoted string, not even by escaping them.

用双引号括起来,shell将扩展 $ _ REQUEST 变量(可能替换为空字符串).

And with double quotes, the shell will expand the $_REQUEST variable (probably substituting the empty string).

尝试一下:

sed -i 's/\$_REQUEST\['\'old\''\]/$_REQUEST['\'new\'']/g' {} + 
# ...................^^...^^..............^^...^^

这些是在单引号字符串块的 外放置的文字单引号.

Those are the literal single quotes placed outside the single quoted string chunks.

或者,用双引号将美元转义:

Or, escape the dollars inside double quotes:

sed -i "s/\\\$_REQUEST\\['old'\\]/\$_REQUEST['new']/g" {} +
# ,.......^.................^.

经过修改,在左侧包含了正则表达式特殊字符所需的转义符

Edited to include the escapes required for the regex-special characters in the left-hand side

这篇关于如何将特殊字符串传递给sed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆