My question is simple as my title says "What is perl's equivalent one liner of grep -o?". That is print the matched regex only instead of whole line?
perl -pe some_option?? 'm/regex/' file
My question is simple as my title says "What is perl's equivalent one liner of grep -o?". That is print the matched regex only instead of whole line?
perl -pe some_option?? 'm/regex/' file
If you want to do the minimal amount of work, change
grep -o -P 'PATTERN' file
to
perl -nle'print $& if m{PATTERN}' file
So from
grep -o -P '(?<=foo)bar(?=baz)' file
you get:
perl -nle'print $& if m{(?<=foo)bar(?=baz)}' file
However, it can be simpler to use a capture.
perl -nle'print $1 if /foo(bar)baz/' file