Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

What is the simplest way to print a field, as in $3 does in awk

by Anonymous Monk
on Oct 23, 2001 at 15:36 UTC ( [id://120752]=perlquestion: print w/replies, xml ) Need Help??

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

What is the simplest method in perl of achieving the following from awk?
awk ' $2 ~ /localhost/ { print $0 } ' /etc/hosts

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: What is the simplest way to print a field, as in $3 does in awk
by jmcnamara (Monsignor) on Oct 23, 2001 at 15:46 UTC
    Use the -a switch to autosplit to @F.
    perl -lane 'print if $F[1] =~ /localhost/' /etc/hosts
    or     perl -lne 'print if /localhost/' /etc/hosts --
    John.
Re: What is the simplest way to print a field, as in $3 does in awk
by projekt21 (Friar) on Oct 23, 2001 at 15:47 UTC
    cat /etc/hosts | perl -ne 'print if (split(/\s+/,$_))[1] =~ localhost'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-24 02:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found