Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep regex to perl or awk

I have been using Linux env and recently migrated to solaris. Unfortunately one of my bash scripts requires the use of grep with the P switch [ pcre support ] .As Solaris doesnt support the pcre option for grep , I am obliged to find another solution to the problem.And pcregrep seems to have an obvious loop bug and sed -r option is unsupported ! I hope that using perl or nawk will solve the problem on solaris.

I have not yet used perl in my script and am unware neither of its syntax nor the flags.

Since it is pcre , I beleive that a perl scripter can help me out in a matter of minutes. They should match over multiple lines .

Which one would be a better solution in terms of efficiency the awk or the perl solution ?

Thanks for the replies .

like image 885
Gil Avatar asked Aug 02 '12 13:08

Gil


2 Answers

These are some grep to perl conversions you might need:

grep -P PATTERN FILE(s) ---> perl -nle 'print if m/PATTERN/' FILE(s)

grep -Po PATTERN FILE(s) ---> perl -nle 'print "$1\n" while m/(PATTERN)/g' FILE(s)

That's my guess as to what you're looking for, if grep -P is out of the question.

like image 198
kevlar1818 Avatar answered Oct 04 '22 02:10

kevlar1818


Here's a shorty:

 grep -P /regex/ ====> perl -ne 'print if /regex/;'
  • The -n takes each line of the file as input. Each line is put into a special perl variable called $_ as Perl loops through the whole file.
  • The -e says the Perl program is on the command line instead of passing it a file.
  • The Perl print command automatically prints out whatever is in $_ if you don't specify for it to print out anything else.
  • The if /regex/ matches the regular expression against whatever line of your file is in the $_ variable.
like image 37
David W. Avatar answered Oct 04 '22 04:10

David W.