Column handling in Perl

[problem]

During my first encounters with Perl many years ago, I asked how can I get a specific column.

It was so easy in AWK! 😉

[/problem]

[solution]

Well here it is in Perl – see example tab.

BTW you can always write an awkscript and run it through a2p – very good for learning Perl! 🙂

[/solution]

[example]

Show column 1:

perl -ane 'print $F[0]."n";'

Show column 2:

perl -ane 'print $F[1]."n";'

Show last column:

perl -ane 'print $F[$#F]."n";'

Show last but one column:

perl -ane 'print $F[($#F-1)]."n";'

So you just run your program, or cat your file, etc and pipe it through this code to get specific column.

[/example]

[reference]

[tags]Perl column handling, Awk to Perl, a2p, Perl Coding School[/tags]

[/reference]

If you have found my website useful, please consider buying me a coffee below 😉

Leave a Reply

Your email address will not be published. Required fields are marked *