Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Awk usage in perl

by jyo123 (Initiate)
on Aug 25, 2008 at 11:52 UTC ( #706668=perlquestion: print w/ replies, xml ) Need Help??
jyo123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Friends, I have a following input file which has 4 rows and 5 columns of data.
1 222 ssss 1111 fgfg 2 333 ssse 2222 dshg 3 444 ssws 3333 sjsh 4 555 wwsw 4444 skss
I have written following awk code to fetch values in each row and particular column like this :
for($s=0;$s<=4;$s++) { $value = `awk 'NR=='$s' {print $4}' comms_input` echo $value; }
I require above code written in perl.Please anyone help on this.

Comment on Awk usage in perl
Select or Download Code
Re: Awk usage in perl
by Corion (Pope) on Aug 25, 2008 at 11:59 UTC

    We all have our different needs. Luckily for you, there is the a2p program distributed with Perl which converts an awk program to Perl.

      Note that the output of a2p will encourage things like $[, which is severely deprecated in Perl and will not always work. Fixing that is beyond the average person who would use a2p and it certainly can't be mixed with other Perl code. Therefore while it was cool and important once, I can't recommend it any more.
Re: Awk usage in perl
by JavaFan (Canon) on Aug 25, 2008 at 12:45 UTC
    You may want to look in the -a, -F and -n command line arguments.
Re: Awk usage in perl
by massa (Hermit) on Aug 25, 2008 at 13:01 UTC
    substitute everything for:
    #!/usr/bin/env perl use strict; use warnings; open my $file, '<', 'comms_input'; while( <$file> ) { chomp; my @fields = split; print "$fields[3]\n" }
    []s, HTH, Massa (κς,πμ,πλ)
Re: Awk usage in perl
by dwm042 (Priest) on Aug 25, 2008 at 16:33 UTC
Re: Awk usage in perl
by repellent (Priest) on Aug 26, 2008 at 08:02 UTC
    Welcome to Perl! You may try the following shell one-liner:
    perl -nale 'print $F[3]' comms_input
    • -n means loop through each line of comms_input
    • -a splits each incoming line into array @F, using whitespace as delimiter
    • -l causes print statement outputs to end with newline "\n"
    • -e defines loop code to be executed after autosplitting is done

    Note: Perl array reference is zero-indexed by default - 'print $F[3]' vs {print $4}
Re: Awk usage in perl
by helgi (Hermit) on Aug 26, 2008 at 15:25 UTC
    use warnings; use strict; while (<DATA>) { chomp; my ($one,$two,$three,$four,$five) = split; print "$five\n"; } __END__ 1 222 ssss 1111 fgfg 2 333 ssse 2222 dshg 3 444 ssws 3333 sjsh 4 555 wwsw 4444 skss

    --
    Regards,
    Helgi Briem
    hbriem AT f-prot DOT com

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://706668]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2014-11-24 01:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (135 votes), past polls