Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl line-mode oneliner with ARGV

Tags:

perl

I often need to run some Perl one-liners for fast data manipulations, like

some_command | perl -lne 'print if /abc/'

Reading from a pipe, I don't need a loop around the command arg filenames. How can I achieve the next?

some_command | perl -lne 'print if /$ARGV[0]/' abc

This gives the error:

Can't open abc: No such file or directory.

I understand that the '-n' does the

while(<>) {.... }

around my program, and the <> takes args as filenames, but doing the next every time is a bit impractical

#/bin/sh
while read line
do
   some_command | perl -lne 'BEGIN{$val=shift @ARGV} print if /$val/' "$line"
done

Is there some better way to get "inside" the Perl ONE-LINER command line arguments without getting them interpreted as filenames?

like image 491
kobame Avatar asked Jan 14 '23 09:01

kobame


1 Answers

Some solutions:

perl -e'while (<STDIN>) { print if /$ARGV[0]/ }' pat

perl -e'$p = shift; while (<>) { print if /$p/ }' pat

perl -e'$p = shift; print grep /$p/, <>' pat

perl -ne'BEGIN { $p = shift } print if /$p/' pat

perl -sne'print if /$p/' -- -p=pat

PAT=pat perl -ne'print if /$ENV{PAT}/'

Of course, it might make more sense to create a pattern that's an ORing or all patterns rather than executing the same command for each pattern.

like image 134
ikegami Avatar answered Jan 25 '23 04:01

ikegami