|Syntactic Confectionery Delight|
Re: One Liner, print multiple regex matchesby kennethk (Abbot)
|on Nov 16, 2012 at 21:43 UTC||Need Help??|
perl -nle ' print "$1$2" if /(?=name=(\w+))(?=age=(\d+))/' filename
Note how your omission of quotes or a comma from your second case means that $1 is being treated at a filehandle.
Your third version fails because you are only allowed one if per statement (see Compound Statements). You could make this work using && and || like:
perl -nle ' /name=(\w+)/ && print($1) || /age=(\d+)/ && print($1) ' filename
or, use two different statements to make it even easier:
perl -nle 'print $1 if /name=(\w+)/; print $1 if /age=(\d+)/' filename
Note that you are using two different regular expressions, so each one would set the $1 buffer if it matches.
Oh, and please wrap input data in <code> tags, as white space is mangled on display if you don't.
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.