简体   繁体   中英

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.

Trying to do a perl one-liner multipattern search replace on a textfile using multipattern file:

textfile:

apples bananas oranges
blueberries walnuts granola

multipatternfile:

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

Using sed I could do this one-liner:

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

Using perl I tried this one-liner:

$ 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.

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.

Ok, I figured this out:

multipatternfile:

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

With perl you need to have semicolons at the ends of the patterns:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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