sed 带有特殊字符

发布于 2024-11-07 04:22:02 字数 349 浏览 0 评论 0原文

我想在这行上使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

久伴你 2024-11-14 04:22:02
sed 's/\$start/\$dsadad/g' your_file
>> ASD = $dsadad ( *.cpp ) 

sed 's/\*//g' your_file
>> ASD = $start ( .cpp ) 

要进行编辑:

sed -i 's/ASD = \$start ( \*.cpp )/ASD = \$dsadad ( .cpp )/' somefile
>> ASD = $dsadad ( .cpp )

添加 -i (--inplace) 来编辑输入文件。

sed 's/\$start/\$dsadad/g' your_file
>> ASD = $dsadad ( *.cpp ) 

sed 's/\*//g' your_file
>> ASD = $start ( .cpp ) 

To follow your edit :

sed -i 's/ASD = \$start ( \*.cpp )/ASD = \$dsadad ( .cpp )/' somefile
>> ASD = $dsadad ( .cpp )

Add the -i (--inplace) to edit the input file.

他不在意 2024-11-14 04:22:02

反斜杠效果很好。 echo '*.cpp' | echo '*.cpp' | echo '*.cpp' | sed 's/\*//' => .cpp

如果您在 shell 中,则可能需要双重转义 $,因为它对于 shell(变量扩展)和 sed(结束行)

echo '$.cpp' | sed "s/\\$//"echo '$.cpp' | sed 's/\$//' => '.cpp'

不要转义 ();这实际上将使它们成为 sed 中的特殊(组)。其他一些常见的字符包括 [ ] \ . ?

这是如何转义的你的例子:

sed 's/ASD = \$start ( \*\.cpp )/ASD = $dsadad ( .cpp )/' somefile

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/\\$//" or echo '$.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:

sed 's/ASD = \$start ( \*\.cpp )/ASD = $dsadad ( .cpp )/' somefile
雨落星ぅ辰 2024-11-14 04:22:02

字符 $*. 对于正则表达式来说是特殊的,因此需要对它们进行转义才能按字面意思理解。

sed 's/ASD = \$start ( \*\.cpp )/ASD = \$dsadad ( .cpp )/' somefile

The chacters $,*,. are special for regular expressions, so they need to be escaped to be taken literally.

sed 's/ASD = \$start ( \*\.cpp )/ASD = \$dsadad ( .cpp )/' somefile
千笙结 2024-11-14 04:22:02

我有一个用例,我必须用变量替换文本,该变量包含一个特殊符号,
例如([电子邮件受保护])。这个 @ 符号造成了一个问题。所以我发现这个解决方案很有用。

SERVICE_ACCOUNT="[email protected]"
sed -i 's/DEVOPS_SERVICE_ACCOUNT/'${SERVICE_ACCOUNT}'/g' file.txt

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.

SERVICE_ACCOUNT="[email protected]"
sed -i 's/DEVOPS_SERVICE_ACCOUNT/'${SERVICE_ACCOUNT}'/g' file.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文