带有特殊字符 (@) 的 sed/perl [英] sed/ perl with special characters (@)

查看:58
本文介绍了带有特殊字符 (@) 的 sed/perl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在这一行上使用 sed/perl:

I have this line that I want to use sed / perl on:

perl -pi -e  "s/password/p@ssw0rd/g" <file_name>

如何让 sed/perl 忽略特殊字符,我尝试在特殊字符前添加反斜杠,但也许我弄错了,有人可以举个例子吗?

How can I make sed/perl ignore special characters, I tried adding back slash before special characters, but maybe I got it wrong, can some one show me an example?

推荐答案

@ 只对 Perl 特殊,不是 sed,所以你可以简单地使用 sed案例:

The @ is only special to Perl, not sed, so you could simply use sed in this case:

sed -i -e 's/password/p@ssw0rd/g' filename     # GNU sed
sed -i '' -e 's/password/p@ssw0rd/g' filename  # BSD sed

当从 shell 向命令传递代码时,你应该养成使用单引号的习惯,因为这样在命令看到之前,shell 不会修改任何代码.如果你这样做,那么反斜杠 at 符号将适用于 Perl:

You should get in the habit of using single-quotes when passing code to commands from the shell, because then none of the code is modified by the shell before the command sees it. If you do that, then backslashing the at sign will work for Perl:

perl -pi -e  's/password/p\@ssw0rd/g' filename

您还可以在替换表达式中使用单引号作为分隔符,这会导致所包含的表达式按字面解释而没有扩展;如果你回到 shell 中的双引号,那会更容易:

You can also use single-quote as the delimiter in the substitution expression, which causes the contained expressions to be interpreted literally with no expansion; that's easier if you go back to double-quotes in the shell:

perl -pi -e "s'password'p@ssw0rd'g" filename

但是如果您可能需要担心那里的shell扩展,您需要用单引号将shell中的代码字符串引用使用单引号作为分隔符,这很尴尬您最好在 at 符号上使用反斜杠:

But if you might need to worry about shell expansion in there, you need to both quote the string of code in the shell with single quotes and use single quotes as the delimiter, which is awkward enough that you're probably better off just going with the backslash on the at sign:

perl -pi -e  's'\''password'\''p@ssw0rd'\''g' filename # POSIX shell
perl -pi -e  $'s\'password\'p@ssw0rd\'g' filename      # bash/ksh/zsh ANSI string

在评论中回答你的问题,如果你想让旧的和新的字符串动态参数作为envariables传递,它们将是 $ENV{varname} 而不仅仅是 $varname 在 Perl 中,你必须区别对待它们.您不再需要担心替换值的插值(因为您只需要它的一个级别),但是您必须担心模式端的特殊正则表达式字符,这意味着您需要放置 \Q...\E 围绕您要查找的内容:

To answer your question in the comments, if you want to make the old and new strings dynamic arguments passed as envariables, they would be $ENV{varname} rather than just $varname in Perl, and you have to treat them differently. You no longer have to worry about interpolation on the replacement value (since you want exactly one level of it), but you do have to worry about special regex characters on the pattern side, which means you need to put \Q...\E around what you're looking for:

CURRENTVALUE=password NEWVALUE=p@ssw0rd perl -pi -e 's/\Q$ENV{CURRENTVALUE}\E/$ENV{NEWVALUE}/g' filename

...也就是说,除非您希望 $CURRENTVALUE 的值能够成为正则表达式而不仅仅是文字字符串.但你必须选择一个.

... that is, unless you want the value of $CURRENTVALUE to be able to be a regex instead of just a literal string. But you have to pick one.

这篇关于带有特殊字符 (@) 的 sed/perl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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