Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Perl equivlanet of simple awk

by Anonymous Monk
on Mar 19, 2011 at 21:57 UTC ( #894193=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

For use in a Perl cgi script, what's an equivalent of the following awk command

awk '/ {print $2}' /home/bob/db.txt

Assume db.txt contains 4 columns, with a valid email address in the first column, and a password in the second column. (For this purpose, security isn't an issue.) I just want to retrieve a password associated with an email address.

Replies are listed 'Best First'.
Re: Perl equivlanet of simple awk
by repellent (Priest) on Mar 19, 2011 at 22:19 UTC
    This equivalent on the command line:
    perl -alne 'print $F[1] if /user\' /home/bob/db.txt

    See Command Switches.

    Expanded code:
    use warnings; use strict; open my $FH, '<', '/home/bob/db.txt' or die $!; while (local $_ = <$FH>) { chomp; print((split ' ')[1], "\n") if /user\; }

    Update: Added expanded code.
Re: Perl equivlanet of simple awk
by wind (Priest) on Mar 19, 2011 at 22:16 UTC
    It's possible to do something like that very easily with a one-liner. However, more verbose is probably what you're looking for if you intend to include this within a script.
    my $file = '/home/bob/db.txt'; open my $fh, $file or die "$file: $!"; my $pass; while (<$fh>) { chomp; my @fields = split /\s+/; if ($fields[0] eq '') { $pass = $fields[1]; last; } } close $fh;
Re: Perl equivlanet of simple awk (a2p)
by LanX (Bishop) on Mar 20, 2011 at 02:17 UTC
    for such conversions you can use a2p

    > echo '/ {print $2}' |a2p #!/usr/bin/perl eval 'exec /usr/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) = split(' ', $_, -1); if (/ { print $Fld2; } }

    anyway you're not checking the first column but the whole line for an email address.

    consider prepending an ^ in your regex.

    Cheers Rolf

    UPDATE: IMHO the @ in the regex should be escaped to avoid array interpolation, seems to be a bug in a2p.

Re: Perl equivlanet of simple awk
by urbansumo (Novice) on Apr 15, 2015 at 11:52 UTC

    Youve just revolutionsied my whole life!! How did I never know about A2p! Thats amazing!

    Thank you so much!

    The Urban Sumo

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://894193]
Approved by ww
[choroba]: the reporter is my colleague, BTW, albeit in a different team

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2018-06-20 12:06 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.