sed 带有特殊字符
我想在这行上使用 sed:
--> ASD = $start ( *.cpp ) <--
其中 $start 不是变量,我想在它上使用 sed 并将所有这一行替换为:
ASD = $dsadad ( .cpp )
如何使 sed 忽略特殊字符,我尝试在特殊之前添加反斜杠字符,但也许我弄错了,有人可以给我举个例子吗?
这就是我想要的:
sed 's/CPPS = \$(shell ls | grep \*\.cpp )/somereplace/' Makefile
I have this line that I want to use sed on:
--> ASD = $start ( *.cpp ) <--
where $start is not a varaiable, I want to use sed on it and replace all this line with:
ASD = $dsadad ( .cpp )
How can I make sed ignore special charactars, I tried adding back slash before special characters, but maybe I got it wrong, can some one show me an example?
Here is what i want :
sed 's/CPPS = \$(shell ls | grep \*\.cpp )/somereplace/' Makefile
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要进行编辑:
添加 -i (--inplace) 来编辑输入文件。
To follow your edit :
Add the -i (--inplace) to edit the input file.
反斜杠效果很好。 echo '*.cpp' | echo '*.cpp' | echo '*.cpp' | sed 's/\*//' =>
.cpp
如果您在 shell 中,则可能需要双重转义
$
,因为它对于 shell(变量扩展)和 sed(结束行)echo '$.cpp' | sed "s/\\$//"
或echo '$.cpp' | sed 's/\$//'
=> '.cpp'不要转义
(
或)
;这实际上将使它们成为 sed 中的特殊(组)。其他一些常见的字符包括[
]
\
.
?
这是如何转义的你的例子:
Backslash works fine.
echo '*.cpp' | sed 's/\*//'
=>.cpp
If you're in a shell, you might need to double escape
$
, since it's a special character both for the shell (variable expansion) and for sed (end of line)echo '$.cpp' | sed "s/\\$//"
orecho '$.cpp' | sed 's/\$//'
=> '.cpp'Do not escape
(
or)
; that will actually make them them special (groups) in sed. Some other common characters include[
]
\
.
?
This is how to escape your example:
字符
$
、*
、.
对于正则表达式来说是特殊的,因此需要对它们进行转义才能按字面意思理解。The chacters
$
,*
,.
are special for regular expressions, so they need to be escaped to be taken literally.我有一个用例,我必须用变量替换文本,该变量包含一个特殊符号,
例如([电子邮件受保护])。这个
@
符号造成了一个问题。所以我发现这个解决方案很有用。I have one use case where I have to replace a text with a variable, that variable contains a special symbol,
like ([email protected]). This
@
symbol was creating an issue. So I found this solution useful.