FAQ
Hi,

I am trying to read a file which is delimited with : .

I only require some information in the first field, and fifth field,

i can easily do it in shell script using
HN=`awk -F: '{print $1}'`
SN=`awk -F: '{print $5}'`

I wonder how can i achieve it using perl?

Search Discussions

  • Jay Savage at Mar 22, 2005 at 4:27 pm

    On Wed, 23 Mar 2005 00:21:47 +0800, ubergoonz wrote:
    Hi,

    I am trying to read a file which is delimited with : .

    I only require some information in the first field, and fifth field,

    i can easily do it in shell script using
    HN=`awk -F: '{print $1}'`
    SN=`awk -F: '{print $5}'`

    I wonder how can i achieve it using perl?

    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    <http://learn.perl.org/> <http://learn.perl.org/first-response>
    my ($hn, $sn) = (split /:/, $your_data)[0,4];

    --jay
  • John W. Krahn at Mar 22, 2005 at 8:01 pm

    ubergoonz wrote:
    Hi, Hello,
    I am trying to read a file which is delimited with : .

    I only require some information in the first field, and fifth field,

    i can easily do it in shell script using
    HN=`awk -F: '{print $1}'`
    SN=`awk -F: '{print $5}'`

    I wonder how can i achieve it using perl?
    HN=`perl -F: -lane'print $F[0]'`
    SN=`perl -F: -lane'print $F[4]'`



    John
    --
    use Perl;
    program
    fulfillment
  • Offer Kaye at Mar 22, 2005 at 8:45 pm

    On Tue, 22 Mar 2005 12:00:39 -0800, John W. Krahn wrote:

    HN=`perl -F: -lane'print $F[0]'`
    SN=`perl -F: -lane'print $F[4]'`
    Hi ubergoonz,
    Just a little addition to John's answer - to understand his solution,
    read "perldoc perlrun" from your command-line, or online at:
    http://perldoc.perl.org/perlrun.html

    It will explain all the flags used in the solutuion (-F, -l, -a, -n and -e).

    --
    Offer Kaye

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedMar 22, '05 at 4:21p
activeMar 22, '05 at 8:45p
posts4
users4
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase