8

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 .

Gil
  • 1,518
  • 4
  • 16
  • 32
  • 2
    Did you try to use the regex with perl? It is worth a try as the name `pcre` does indicate certain Perl compatibility. – matthias krull Aug 02 '12 at 13:13
  • Have you checked to see if your system has the GNU grep installed under an alternate path such as `/usr/gnu/bin`, `/usr/sfw/bin`, `/usr/local/bin`, `/opt/csw/bin`, or similar? Then you'd just need to change the path to grep in your script, not rewrite it to a new command. – alanc Aug 04 '12 at 01:34

2 Answers2

5

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.

kevlar1818
  • 3,055
  • 6
  • 29
  • 43
  • You mean `perl -nle 'print if /PATTERN/' FILES`. – tchrist Aug 02 '12 at 13:19
  • Thanks, yeah just ran some tests and came to that conclusion! Next time, feel free to edit. – kevlar1818 Aug 02 '12 at 13:22
  • I believe that the second one should be more like: `perl -nle 'print $1 while /(PATTERN)/g'` – Dimitre Radoulov Aug 02 '12 at 13:48
  • I would also prefer `perl -nlE 'say $1 if /(PATTERN)/' FILE` for the second for brevity. – zostay Aug 02 '12 at 13:57
  • Ok, it's all sorted out. Thanks for the additions. – kevlar1818 Aug 02 '12 at 14:14
  • @kevlar1818 Unfortunately it doesnt work for the same regex :( but I tried with another regex and it works great ! I have now added a sample i/p file . – Gil Aug 02 '12 at 14:21
  • Some versions of Perl don't have `say` or require that it be explicitly enabled. The `-l` option makes it unnecessary since it automatically sets `$\ = "\n"` so `print` will output newlines. – Dennis Williamson Aug 02 '12 at 15:35
  • I cant seem to match the `perl` code for the pattern mentioned in the question , over multiple lines [single line matches are ok ] ! Am I missing something here ? – Gil Aug 02 '12 at 18:33
  • `perl -0777pe 'print $1 while /(pattern with\nnewlines)/g'`. See also the `/m` and `/s` options, although I imagine they should be familiar from PCRE land. The basic issue here is that Perl runs the script on one input line at a time by default. – tripleee Aug 03 '12 at 19:41
2

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.
David W.
  • 105,218
  • 39
  • 216
  • 337