Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery


by rgiskard (Hermit)
on Feb 24, 2005 at 19:29 UTC ( #434207=perlquestion: print w/replies, xml ) Need Help??
rgiskard has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to transition out of using awk for the usual column grabbing. What's the best(shortest/prettiest) perl way to replace my addiction to:
     gawk '{print $1}'
as in the example:

    echo "best ever" | gawk '{print $1}'

Replies are listed 'Best First'.
Re: awk...ward
by jdporter (Canon) on Feb 24, 2005 at 19:38 UTC
    perl -lane "print $F[1]"

    See perldoc perlrun.

    Update: On Windows, you have to use double-quotes to enclose a command-line script. On Unix, there'll be a problem with the double-quotes and the dollar sign. Therefore, on Unix, I'd recommend using single-qoutes if possible; or otherwise, escape the dollar sign.

      perl -lane "print $F[1]"
      or if we're golfing here,
      perl -lape'$_=$F[1]'


      Missed it by that much.</Maxwell Smart> YM $F[0]; awk's fields are 1-based ($0 being the entire line).

        D'oh. Well, I'm not ashamed to admit my awk skills are a little rusty. Perl has been my text processing tool of first resort for the last... oh, 11 years. :-)
Re: awk...ward
by Roy Johnson (Monsignor) on Feb 24, 2005 at 20:28 UTC
    The a2p program that is included with perl will translate awk scripts to perl. It can be instructive to look at the translations, if your original script wasn't too complex. (For complex awk scripts, the translation soon gets too cumbersome to plow through.)
    % cat try.awk {print $1} % a2p try.awk > try.perl % cat try.perl #!/usr/shell/bin/perl eval 'exec perl -S $0 "$@"' 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 $, = ' '; # set output field separator $\ = "\n"; # set output record separator while (<>) { ($Fld1) = split(' ', $_, 9999); print $Fld1; }

    Caution: Contents may have been coded under pressure.
      Except in this case. That's some pretty nasty perl for what it's trying to do. I've previously contemplated writing a module to somehow allow awk column sementics in perl... never quite got a round tuit though.

      I'm not belgian but I play one on TV. On dit que je parle comme un belge aussi.

        Inline::Awk gives you all the power (?!) of awk from within a Perl program:
        perl -le 'use Inline Awk=>q({print $1}); awk()' file

        I agree with you that the output of a2p isn't pretty and it is probably not the best way for an awk programmer to learn Perl.


Re: awk...ward
by friedo (Prior) on Feb 24, 2005 at 19:34 UTC
    To split on whitespace you can use (*grin*) split.

    perl -le 'print((split " ", "best ever")[0])'

    Or, you could grab it with a regex.

    perl -le '"best ever" =~ /(\w+)/; print $1'
Re: awk...ward
by eyepopslikeamosquito (Chancellor) on Feb 24, 2005 at 22:58 UTC

    Though I generally much prefer Perl to awk, in this case I prefer your awk one-liner above to the equivalent Perl one-liner -- if calling from a /bin/sh script. My main suggestion is to write the whole script, not in /bin/sh, but Perl; then you don't need all these little one-liners. The only time I write /bin/sh over Perl is for truly tiny scripts or when I need the script to run on a machine that does not have Perl installed.

Re: awk...ward
by RazorbladeBidet (Friar) on Feb 24, 2005 at 19:35 UTC

    perl -le 'print ((split " ", "best ever")[0])'

    Alas, too late... ok how about this (to combine with the echo)

    echo "best ever" | perl -le 'print ((split " ", <STDIN>)[0])'
    It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs
Re: awk...ward
by ambrus (Abbot) on Dec 26, 2007 at 11:04 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://434207]
Approved by friedo
[hippo]: Please also take a moment to read through I know what I mean. Why don't you? - this guidance will help others to help you.

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2018-02-21 10:21 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (277 votes). Check out past polls.