Home > Archive > PERL Beginners > March 2005 > perl's awk function
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
perl's awk function
|
|
| Ubergoonz 2005-03-22, 3:56 pm |
| 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?
| |
| Jay Savage 2005-03-22, 3:56 pm |
| On Wed, 23 Mar 2005 00:21:47 +0800, ubergoonz <ubergoonz@gmail.com> 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 2005-03-22, 8:55 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 2005-03-22, 8:55 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
|
|
|
|
|