简体   繁体   English

使用perl脚本命令在多种文本文件中查找和替换特殊字符串

[英]Find and replace special character string in mulitiple text file using perl script command

I am using the below single line command for replacing words 我正在使用以下单行命令替换单词

perl -i -p -e 's/old/new/g;' *.config

which works fine for normal string. 这对于正常的字符串工作正常。 However if i want to replace a string with special characters like below it doesn't work. 但是,如果我想用以下特殊字符替换字符串,则不起作用。

perl -i -p -e 's/{{'TEXT' | translate}}/{{'TEXT.T.D' | translate}}/g;' *.config

oldText will be like = {{'TEXT' | translate}} oldText将类似于= {{'TEXT' | translate}} {{'TEXT' | translate}}

New text will be like = {{'TEXT.TD' | translate}} 新文本将类似于= {{'TEXT.TD' | translate}} {{'TEXT.TD' | translate}}

The | | symbol is a metacharacter in regular expressions (it means "or"). symbol是正则表达式中的元字符(表示“或”)。 In order to use it to mean itself, you need to escape it with a backslash. 为了使用它来表示自己,您需要使用反斜杠将其转义。

s/{{'TEXT' \| translate}}/{{'TEXT.T.D' | translate}}/g

Another alternative (which will escape all potentially problematic characters in the regex) is to use the \\Q ("quotemeta") escape sequence. 另一个替代方法(将转义正则表达式中所有可能有问题的字符)是使用\\Q (“ quotemeta”)转义序列。

s/\Q{{'TEXT' | translate}}/{{'TEXT.T.D' | translate}}/g

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM