Perl equivalent of simple awk script

Discussion in 'Perl Misc' started by Generic Usenet Account, Nov 12, 2008.

  1. 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
     
    Generic Usenet Account, Nov 12, 2008
    #1
    1. Advertising

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

    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



    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/"
     
    Tad J McClellan, Nov 12, 2008
    #2
    1. Advertising

  3. 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
     
    John W. Krahn, Nov 12, 2008
    #3
  4. 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
     
    John W. Krahn, Nov 12, 2008
    #4
  5. Generic Usenet Account

    Guest

    On Wed, 12 Nov 2008 13:01:46 -0800 (PST), Generic Usenet Account <> 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
     
    , Nov 13, 2008
    #5
  6. Generic Usenet Account

    Guest

    On Wed, 12 Nov 2008 13:01:46 -0800 (PST), Generic Usenet Account <> 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
     
    , Nov 16, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. engineer

    Tool for perl script to awk

    engineer, Jun 14, 2004, in forum: Perl
    Replies:
    1
    Views:
    926
    Joe Smith
    Jun 14, 2004
  2. Matthew Thorley

    python vs awk for simple sysamin tasks

    Matthew Thorley, Jun 3, 2004, in forum: Python
    Replies:
    20
    Views:
    1,664
    Donald 'Paddy' McCarthy
    Jun 5, 2004
  3. Perl script to replace awk

    , Jul 26, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    150
    Geoffroy Braem
    Jul 27, 2004
  4. AyOut

    Executing awk from perl script

    AyOut, Mar 24, 2007, in forum: Perl Misc
    Replies:
    6
    Views:
    171
    Greg Bacon
    Mar 26, 2007
  5. Replies:
    5
    Views:
    797
    John W. Krahn
    Apr 2, 2007
Loading...

Share This Page