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'