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

How do I "awk print $2"? I open a file with lots of unique lines (diskpart.exe list disks) I qualify the lines I want with a =~ match (this is working) I want to trim the line, and access the second word; (awk print $2) Everything I find in my O'Reilly's are literal byte indexes or pre/nicely-formatting input lines. My input lines have varying numbers of spaces in between the words which might be digits. Once I can get $2 I'll hash and use number of hashes as control to another loop to populate another array. I am hoping someone can show me how to trim() and split() then access via index(N) the line. Thanks,

Replies are listed 'Best First'.
Re: HowDoI parse the line
by duff (Parson) on Mar 30, 2006 at 22:35 UTC

    Um, if you have awk, use it. TMTOWTDI includes those ways that don't involve perl. That said, the following is equivalent to your awk line:

    perl -lane 'print $F[1]'
Re: HowDoI parse the line
by Tanktalus (Canon) on Mar 30, 2006 at 22:33 UTC

    Did you check the docs for split? Use "split ' ', $line" to split the same way that awk does.

Re: HowDoI parse the line
by graff (Chancellor) on Mar 31, 2006 at 04:49 UTC
    From what you're saying, I think you want an expression like this (elaborating a little bit on the first reply):
    while (<>) [ my $second_token = ( split " " )[1]; # do something with the second token... }