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

Hi , Does perl has a feature similar to awk to handle fields like $1,$2 etc. I want only the value of ceratin fields from each line a file. how do I get it in perl?

Replies are listed 'Best First'.
Re: using fields in perl
by hsinclai (Deacon) on Dec 25, 2007 at 04:14 UTC
    Yes, autosplit :)

    perl -ane 'print $F[0] . $/;' FILE is close to what you'd get with awk '{print $1}' FILE

    Do perldoc perlrun, look for -a for the gory details.

        You guys, I'm not sure '-a' is quite equivalent to the awk fields. I'm no good at awk, but I believe it splits on m/\s+/ rather than \s. perlrun says '-a' is this:
        while (<>) { @F = split(' '); print pop(@F), "\n"; }

        But I think you'd have to @F = grep {$_} @F or change the split or something.

        no?

        (Also, I think elan should work. I mean, I know it doesn't, but isn't there some posix-y arg processing style where if 'a' and 'b' take arguments "-dacbe aarg barg" still works? Perhaps I imagined that.)

        UPDATE: oh, jstrom, I did *not* know that... I assumed split splooged any argument into a regex.

        -Paul

Re: using fields in perl
by rgiskard (Hermit) on Dec 26, 2007 at 02:03 UTC
Re: using fields in perl
by planetscape (Chancellor) on Dec 26, 2007 at 11:47 UTC