Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

awk like question in perl

by drock (Beadle)
on Aug 11, 2004 at 18:28 UTC ( #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 (Archbishop) 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
Node Status?
node history
Node Type: perlquestion [id://382045]
Approved by saberworks
help
Chatterbox?
[marto]: I suspect more trolling...
[usemodperl]: Because the audience for my apps have old, locked down versions of perl that can't install modules or update perl and now all that is horribly broken without HTTP. Of course I can go outside Perl anytime but that impacts portability
[usemodperl]: I can find cpan mirrors on http but not with plain files, only tar.gz, does something like meta exist on http?
[marto]: run your own mirror minicpan
[marto]: or just fatpack/package your apps
[Corion]: usemodperl: No. I think your approach to "downloading modules" is horribly misguided. I recommend you use the standard tools, and/or move your own tools to standard interfaces. Or maybe just set up your own plaintext mirror if that's what you need.
[Corion]: usemodperl: If your "audience" runs old, locked-down computers that can connect to the outside world, they shouldn't install random stuff from CPAN. Especially, as many modules on CPAN don't work with 5.005_004 anymore.
[usemodperl]: downloading modules is wrong? oh dear...
[usemodperl]: just answer my question without judging me or my technique, if that's possible around here anymore, sheesh

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2018-06-24 16:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (126 votes). Check out past polls.

    Notices?