Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Perl equivalent of simple awk script

3,566 views
Skip to first unread message

Generic Usenet Account

unread,
Nov 12, 2008, 4:01:46 PM11/12/08
to
Hi,

I am an old hat who is still living in the grep, sed, awk era. I am
trying to come up to speed with Perl since I am told that my toolkit
is completely outdated. I would appreciate if someone could help me
with a Perl equivalent of the following extremely simple awk command:

awk '/pattern/ {print $1 "-" $2 "-" $3 "\t" $NF}' inputfile

Thanks,
Larry

John W. Krahn

unread,
Nov 12, 2008, 4:31:16 PM11/12/08
to

perl -lane'/pattern/ && print "$F[0]-$F[1]-$F[2]\t$."' inputfile

John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Tad J McClellan

unread,
Nov 12, 2008, 4:26:33 PM11/12/08
to
["Followup-To:" header set to comp.lang.perl.misc.]


The perl distribution ships with an awk-to-perl translater:

man a2p

When I try it on your program, it outputs:

---------------------------
#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
if $running_under_some_shell;
# this emulates #! processing on NIH machines.
# (remove #! line above if indigestible)

eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
# process any FOO=bar switches

$[ = 1; # set array base to 1
$, = ' '; # set output field separator
$\ = "\n"; # set output record separator

while (<>) {
@Fld = split(' ', $_, -1);
if (/pattern/) {
print $Fld[1] . '-' . $Fld[2] . '-' . $Fld[3] . "\t" . $Fld[$#Fld];
}
}
---------------------------


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

John W. Krahn

unread,
Nov 12, 2008, 6:41:39 PM11/12/08
to
John W. Krahn wrote:
> Generic Usenet Account wrote:
>>
>> I am an old hat who is still living in the grep, sed, awk era. I am
>> trying to come up to speed with Perl since I am told that my toolkit
>> is completely outdated. I would appreciate if someone could help me
>> with a Perl equivalent of the following extremely simple awk command:
>>
>> awk '/pattern/ {print $1 "-" $2 "-" $3 "\t" $NF}' inputfile
>
> perl -lane'/pattern/ && print "$F[0]-$F[1]-$F[2]\t$."' inputfile

Correction, that should be:

perl -lane'/0/ && print "$F[0]-$F[1]-$F[2]\t$F[-1]"' inputfile

s...@netherlands.com

unread,
Nov 12, 2008, 8:02:57 PM11/12/08
to


Awk says the pattern can be a conditional on the field data,
so it probably splits it up first. Although there is a $0 record
variable, so it depends on what when and where you wan't to
match.

Here is some very simple examples. Complex splitting can be done
using regular expressions for delemeter determination as well.
You can also forego split and parse the record fields with
a custom regular expression. I always thought DB's were
a better processor for records though.

Welcome to Perl.

sln

------------------------


use strict;
use warnings;

my @fld = ();
my ($pattern, $record) = ('w', '');

while ($record = <DATA>)
{
if ($record =~ /$pattern/)
{
# split fields up on white spaces (like awk)
@fld = split (' ', $record);

# do error checking, expected number of fields,
# more matching of field data, etc..
# ..

# print out some stuff
print "#1 - $fld[0]-$fld[1]-$fld[2]\t$fld[$#fld]\n";
}

# or
if ((@fld = split (' ', $record)) > 3 && $fld[3] eq $pattern)
{
print "#2 - $fld[0]-$fld[1]-$fld[2]\t$fld[$#fld]\n";
}

# or
if ((@fld = split (' ', $record)) > 3 && $fld[3] =~ /$pattern/)
{
print "#3 - $fld[0]-$fld[1]-$fld[2]\t$fld[$#fld]\n";
}

# or
if ((@fld = split (' ', $record)) > 3 && $fld[2] == 2007 && $fld[3] =~ /$pattern/)
{
print "#4 - $fld[0]-$fld[1]-$fld[2]\t$fld[$#fld]\n";
}

# or
if ((@fld = split (' ', $record)) > 4)
{
print "#5 - there are ".@fld." fields in this record\n";
}
}

__DATA__

Jan 20 2007 w
Jan 21 2007 w
Jan 22 2007 l
Jan 23 2008 w
Jan 24 2008 l
Jan 25 2008 l
Jan 26 2008 w extra
Jan 27 2008 l extra
Jan 28 2008 w extra


s...@netherlands.com

unread,
Nov 16, 2008, 3:55:34 PM11/16/08
to
On Wed, 12 Nov 2008 13:01:46 -0800 (PST), Generic Usenet Account <use...@sta.samsung.com> wrote:

Larry,

So I guess you learned about Perl's advanced split() function.
How's the toolbox looking now?


sln

0 new messages