Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl equivalent of simple awk script

Reply
Thread Tools

Perl equivalent of simple awk script

 
 
Generic Usenet Account
Guest
Posts: n/a
 
      11-12-2008
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
 
Reply With Quote
 
 
 
 
Tad J McClellan
Guest
Posts: n/a
 
      11-12-2008
["Followup-To:" header set to comp.lang.perl.misc.]

Generic Usenet Account <(E-Mail Removed)> 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



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/"
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      11-12-2008
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



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
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      11-12-2008
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


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
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      11-13-2008
On Wed, 12 Nov 2008 13:01:46 -0800 (PST), Generic Usenet Account <(E-Mail Removed)> wrote:

>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


 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      11-16-2008
On Wed, 12 Nov 2008 13:01:46 -0800 (PST), Generic Usenet Account <(E-Mail Removed)> wrote:

>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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to put the command ls -l| awk '{print $1" "$5" "$9}' in a perl script? robertchen117@gmail.com Perl Misc 5 04-02-2007 03:43 PM
Executing awk from perl script AyOut Perl Misc 6 03-26-2007 01:49 PM
Perl script to replace awk laura.hradowy@mts.NOSPAM.ca Perl Misc 3 07-26-2004 11:12 PM
Tool for perl script to awk engineer Perl 1 06-14-2004 07:00 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments