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


Hey guys, I just need an example of how to express something in Perl the way I know how to do it with awk.

Ideally I would like to do something like this:

my $pSWP = `awk '/something/ { print $3 }' /dir/file`;

But the value of $pSWP appears to be null when I can confirm that the awk command should return a value when ran on the system. Apparently doing something like this is kind of a no-no and I can do the same thing in Perl but I'm not sure what all is available to do this in only Perl :(

Replies are listed 'Best First'.
Re: Using Perl instead of awk!
by eyepopslikeamosquito (Archbishop) on Sep 30, 2014 at 19:25 UTC

    Something like this should get you started:

    use strict; use warnings; my $fname = '/dir/file'; open my $fh, '<', $fname or die "error: open '$fname': $!"; while (<$fh>) { next unless /something/; my @flds = split; print "$flds[2]\n"; }
    See open and split.

Re: Using Perl instead of awk!
by choroba (Cardinal) on Sep 30, 2014 at 19:33 UTC
    Backquotes interpolate variables, and $3 is a special variable in Perl. So, Perl interpolates it (but it's probably empty, do you use warnings?) before awk can see it. You can prevent interpolation by backslashing the dollar sign. But... Why would you call awk from Perl? You can get the same result without shelling out.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Using Perl instead of awk!
by runrig (Abbot) on Sep 30, 2014 at 21:04 UTC
    The result is more verbose than it needs to be, but the last part is fairly straightforward:
    $ a2p <<'EOT' > /something/ { print $3 } > EOT #!/usr/local/bin/perl eval 'exec /appl/toolbox/perl5.8.8/bin/perl -S $0 ${1+"$@"}' if $running_under_some_shell; # this emulates #! processing on NIH machines. # (remove #! line above if indigestible) eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift; # process any FOO=bar switches $, = ' '; # set output field separator $\ = "\n"; # set output record separator while (<>) { ($Fld1,$Fld2,$Fld3) = split(' ', $_, -1); if (/something/) { print $Fld3; } }
    Updated/fixed...single quoted 'EOT'
Re: Using Perl instead of awk!
by Tux (Canon) on Oct 01, 2014 at 06:31 UTC

    When you do know awk, and want to see/learn how the same thing is done in perl, you can use the awk-to-perl tool a2p that shipped with perl. The generated perl code is probably (very) inefficient, but at least shows you how the conversion could be done:

    $ echo '/something/ { print $3 }' | a2p #!/pro/bin/perl eval 'exec /pro/bin/perl -S $0 ${1+"$@"}' if $running_under_some_shell; # this emulates #! processing on NIH machines. # (remove #! line above if indigestible) eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift; # process any FOO=bar switches $, = ' '; # set output field separator $\ = "\n"; # set output record separator while (<>) { ($Fld1,$Fld2,$Fld3) = split(' ', $_, -1); if (/something/) { print $Fld3; } }

    Enjoy, Have FUN! H.Merijn
Re: Using Perl instead of awk!
by karlgoethebier (Abbot) on Sep 30, 2014 at 19:24 UTC

    Not sure if i got it... but turn on auto split

    karls-mac-mini:monks karl$ echo foo bar nose cuke > file karls-mac-mini:monks karl$ echo foo bar nose cuke >> file karls-mac-mini:monks karl$ cat file foo bar nose cuke foo bar nose cuke # bullshit! perl -lnae 'print $F[3];' file perl -lne '/(cuke)/;print $1' file cuke cuke karls-mac-mini:monks karl$

    Update: Shit! Did you mean this?

    See perlrun.

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: Using Perl instead of awk!
by codiac (Beadle) on Oct 01, 2014 at 01:56 UTC
    For your code to work you need to escape the '$' awk is using:
    my $pSWP = `awk '/something/ { print \$3 }' /dir/file`;
    But like others said, why use awk when you can do it in perl :)