Reputation: 2635
If I use Perl regex (PCRE) with GNU grep
, it works pretty well; please consider:
$ echo FOO | grep -P '(?i)foo'
FOO
I tried PCRE with awk
but awk
does not support PCRE.
I only found three different regex modifiers for awk
/gawk
--posix
--traditional
--re-interval
Does any of the awk
family use Perl regex (PCRE) like the grep -P
example above?
Upvotes: 6
Views: 5749
Reputation: 34120
Why don't you just learn Perl?
Which can do everything you can do in Awk, Sed, and bash. ( and then some )
$ echo FOO | grep -P '(?i)foo'
FOO
$ echo FOO | perl -ne 'print if /(?i)foo/'
FOO
$ ls /bin /usr/bin | perl -lnE '$p{$_}++ if /(grep|sed|awk)$/; END { say for sort keys %p }'
awk
bzegrep
bzfgrep
bzgrep
dgawk
egrep
fgrep
gawk
grep
igawk
list-unreleased
lzegrep
lzfgrep
lzgrep
mawk
msggrep
nawk
pgawk
pgrep
psed
ptargrep
rgrep
sed
xzegrep
xzfgrep
xzgrep
zegrep
zfgrep
zgrep
zipgrep
#! /usr/bin/env perl
use v5.12.0; # sets minimum version adds `say`, and does `use strict`
use warnings;
use File::Spec::Functions qw' path catfile splitpath ';
my %cmds;
# go through all directories in `$PATH` or `%PATH%`
# in a platform independent way
for my $dir_path ( path ){
# a quick hack that is good enough for now
for my $cmd_path ( glob catfile $dir_path, '*grep' ){
# only need the filename, so assign everything else
# to `undef`
my ( undef, undef, $cmd ) = splitpath $cmd_path;
# store it in an array in the %cmds hash
push @{ $cmds{$cmd} }, $cmd_path;
}
}
for my $cmd ( sort keys %cmds ) {
say $cmd, ' :';
# the actual locations of the program or symlink
say ' 'x4, $_ for @{ $cmds{$cmd} };
}
bzegrep :
/bin/bzegrep
bzfgrep :
/bin/bzfgrep
bzgrep :
/bin/bzgrep
egrep :
/bin/egrep
fgrep :
/bin/fgrep
grep :
/bin/grep
lzegrep :
/usr/bin/lzegrep
lzfgrep :
/usr/bin/lzfgrep
lzgrep :
/usr/bin/lzgrep
msggrep :
/usr/bin/msggrep
pgrep :
/usr/bin/pgrep
ptargrep :
/opt/perl/bin/ptargrep
/usr/bin/ptargrep
rgrep :
/usr/bin/rgrep
xzegrep :
/usr/bin/xzegrep
xzfgrep :
/usr/bin/xzfgrep
xzgrep :
/usr/bin/xzgrep
zegrep :
/bin/zegrep
zfgrep :
/bin/zfgrep
zgrep :
/bin/zgrep
zipgrep :
/usr/bin/zipgrep
I would like to point out the reason I have ptargrep
in there twice is that I maintain my own versions of Perl with one of them symlinked from /opt/perl/bin
, and ptargrep
is a Perl program that installs with the Archive::Tar module.
This example doesn't even use any of the cool new features added to Perl since 2010 when 5.12.0 was released.
( Perl now has a yearly release cycle )
Of course you could also use Perl to create a website like Lacuna Expanse, or to write a video game like Frozen Bubble.
There is also a new language called Perl 6 which I find even more fun to use. The plan is for there to be a full release sometime this year.
Upvotes: 2
Reputation: 203189
No, awk does not support Perl REs. One of the primary tenets of the awk language is to keep the language small by not providing constructs to do simple things a bit more briefly. That's how they avoid the language bloat and associated complexity that perl suffers from (see http://www.zoitz.com/archives/13 for the common perception of perl readability). In this case while some of the Perl RE syntax lets you do some things a bit briefer, you don't NEED special language constructs to do them as there's other ways to do whatever you want with existing constructs (maybe not in one RE or using one function so not as briefly) so awk doesn't support them.
Upvotes: 12