Perl equivalent of simple awk script

  • Thread starter Generic Usenet Account
  • Start date
G

Generic Usenet Account

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
 
T

Tad J McClellan

["Followup-To:" header set to comp.lang.perl.misc.]

Generic Usenet Account said:
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


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];
}
}
 
J

John W. Krahn

Generic said:
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



John
 
J

John W. Krahn

John said:
Generic said:
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


John
 
S

sln

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


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

sln

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

Larry,

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


sln
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top