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

Monks, I'm trying to capture a specific column of data from a logfile. In the past I've used Awk. With Perl I have the following:
while (<>) { my @temp = split / /; print $temp[4],"\n"; }
Can someone suggest a cleaner way to grab a column? Maybe with regex. In this case the columns are seperated by a single space. Thanks.

Replies are listed 'Best First'.
Re: Column help
by tachyon (Chancellor) on Oct 16, 2004 at 21:46 UTC

    What's wrong with what you have. You can make it shorter. cut -f5 infile > outfile springs to mind. You can use split to create an anonymous array and then select the element(s) you want from it all in the print. You might call it cleaner, you would probably not call it clearer for the non initiated. The flags are -e execute, -n read line by line into $_, -l automatically deal with newlines on IO.

    perl -nle 'print ((split)[4])' infile > outfile

    cheers

    tachyon

Re: Column help
by ihb (Deacon) on Oct 16, 2004 at 21:13 UTC

    You could do

    perl -awnle 'print $F[4]'
    if you want it as a one-liner. Beware that it splits with the special ' ' delimiter which isn't identical to / / (see split). If you really need to split on a single space, consult the perlrun manpage for the -F switch which sets split pattern and use \x20.

    Other than that, I think it's clear enough.

    ihb

    Read argumentation in its context!

Re: Column help
by pg (Canon) on Oct 16, 2004 at 21:29 UTC

    See comments:

    use warnings; use strict; my $str = "12345678"; my @col = split //, $str; #not / / print $col[3], "\n"; #zero based $str =~ /.{3}(.)/; print $1;
Re: Column help
by mhearse (Chaplain) on Oct 16, 2004 at 22:39 UTC
    Thanks for the input.
Re: Column help
by stevecomrie (Beadle) on Oct 18, 2004 at 13:48 UTC
    while ( <> ) { $_ =~ s|^(.*?\s){3}([^\s]*).*$|$2|; print $_ . "\n"; }
    Appears to be about twice as fast as using split. At least, in my benchmark.
      Could you explain your regex? Is this a substitution?