带有特殊字符 (@) 的 sed/perl

编程入门 行业动态 更新时间:2024-10-27 14:27:59
本文介绍了带有特殊字符 (@) 的 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

本文发布于:2023-11-16 11:55:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1604062.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:特殊字符   sed   perl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!