简体   繁体   English

是否存在等效于以下内容的单行perl:sed -n -f multipatternfile文本文件?

[英]Is there a one-line perl equivalent to: sed -n -f multipatternfile textfile?

I've been searching for hours for a one-liner perl command to put in a script that will do multipattern replacements read from a file similar to sed. 我一直在寻找数小时的单线perl命令,以放入一个脚本,该脚本将执行与sed相似的文件读取的多模式替换。

Trying to do a perl one-liner multipattern search replace on a textfile using multipattern file: 尝试使用multipattern文件在文本文件上执行perl单线multipattern搜索替换:

textfile: 文本文件:

apples bananas oranges
blueberries walnuts granola

multipatternfile: multipatternfile:

s/blueberries/chocolate/p
s/bananas/raisins/p

Using sed I could do this one-liner: 使用sed我可以做到这一点:

$ sed -n -f multipatternfile  textfile
apples raisins oranges
chocolate walnuts granola

Using perl I tried this one-liner: 使用perl,我尝试了以下这种方法:

$ perl -pi -F multipatternfile  textfile
syntax error at multipatternfile line 2, near "s/bananas/raisins/p"
Execution of multipatternfile aborted due to compilation errors.

But the perl version chokes at the second pattern no matter what I have in there for the second pattern. 但是,无论我在第二种模式中拥有什么,perl版本都会使第二种模式令人窒息。

Is there something wrong with the way I have the pattern file formatted? 我将格式文件格式化的方式有问题吗?

Before you ask, I cannot use sed because sed has no non-greedy regex. 在您问之前,我无法使用sed,因为sed没有非贪婪的正则表达式。

Ok, I figured this out: 好的,我知道了:

multipatternfile: multipatternfile:

s/blueberries/chocolate/p;
s/bananas/raisins/p;

With perl you need to have semicolons at the ends of the patterns: 使用perl,您需要在模式的末尾添加分号:

$ perl -p multipatternfile textfile
apples raisins oranges
chocolate walnuts granola

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

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