Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

awk like question in perl

by drock (Beadle)
on Aug 11, 2004 at 18:28 UTC ( [id://382045]=perlquestion: print w/replies, xml ) Need Help??

drock has asked for the wisdom of the Perl Monks concerning the following question:

All, is there a way to tell perl to give me only these fields as in awk '{print $1" "$2" "$6}' I want the date field, the time field and the Estring field. thank you! derek Here is my data file:
08/09/04 11:02:43 [ 21587:ebexpire, ebexpire.c@7475] E00698 9840S 3 +59 2303DE53C80C29 08/09/04 11:02:43 [ 21587:ebexpire, ebexpire.c@7475] E00237 9840S 4 +31 E603DE70640F23 08/09/04 11:02:43 [ 21587:ebexpire, ebexpire.c@7475] E00693 9840S 3 +54 4303DE8D86F7EA 08/09/04 11:02:43 [ 21587:ebexpire, ebexpire.c@7475] E00118 9840S 1 +16 503DEA7 FAF83C

Replies are listed 'Best First'.
Re: awk like question in perl
by Roy Johnson (Monsignor) on Aug 11, 2004 at 18:48 UTC
    Perl comes with a program called "a2p" that will translate awk scripts into perl equivalents.

    For your example, this should do what you want:

    perl -na 'print "@F[0,1,5]\n"'

    Caution: Contents may have been coded under pressure.
      ok so how would I use this against a perl script ? is it
      perl -na script.pl 'print "@F[0,1,5]\n"'

        No. You'd either pipe the output of one script through the other:

        perl script.pl | perl -nae'print "@F[0,1,5]\n"'

        which is the same as doing it with awk.

        Or you'd have to edit script.pl to change the statements that print its output to do what you want.

        Makeshifts last the longest.

Re: awk like question in perl
by davido (Cardinal) on Aug 11, 2004 at 18:48 UTC

    That looks like fixed-width data. Use unpack (I think the documentation for unpack templates is under pack.

    You can also split on whitespace (so it seems), and take a slice of the 2nd and 6th items from the return list. But for fixed-column-width input, I would prefer the reliability of unpack.


    Dave

      I agree that it looks like fixed-width, for which the tools of choice are pack and unpack. But the default behavior in awk is to split on whitespace, so split is more likely to do what the OP wanted.

        I kind of figured splitting on whitespace was the original objective, but I suggested unpack as what I thought might be a better alternative. However, split is pretty simple to use, so here's an example that pulls out the 2nd and 6th fields:

        { local $, = "\t"; print +(split)[1,5], $/ while <DATA>; }

        HTH!


        Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://382045]
Approved by saberworks
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-03-28 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found