On Thursday 10 July 2008 05:59:36 Anirban Adhikary wrote:
> Dear list
> I want to capture the output of w and then I want to do some job as per the
> o/p of w command in my linux system. So i have written the code as follows
>
> use strict;
> use warnings;
>
> open (LS, "w|") or die "can't open w: $!";
> my @arr = <LS>;
> close (LS);
> shift @arr;
> shift @arr;
> my($one,$two,$three,$four,$five,$six,$seven,$eight);
>
> foreach my $el(@arr)
>  {
>   ($one,$two,$three,$four,$five,$six,$seven,$eight) = split(/ /,$el);
>    print $five."\n";
>  }
> but the problem is as per the o/p of w command I want to extract the fifth
> field but by using the above code I am not able to do this. The o/p of who
> command in my system is as follows
>
> USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
> bighosh  pts/1    bighosh.in.conne 13:48   38:03   0.16s  0.06s sshd:
> bighosh [priv]
> nchatter pts/2    nchatterjee.in.c 10:31   15:38   0.68s  0.65s -bash
> anadhika pts/3    anadhikary.in.co 15:10    0.00s  0.04s  0.00s w
> suray    pts/4    suray1.in.connec 12:36   46:43   0.04s  0.04s -bash
> merge    pts/5    sasarkar.in.conn 12:00    5:44   0.11s  0.11s -bash
> dpghosh  pts/6    dpghosh.in.conne 12:09    1:32   0.07s  0.01s sshd:
> dpghosh [priv]
> anadhika pts/7    anadhikary.in.co 13:50   10.00s  0.12s  0.12s -bash
>
> I need to extract the idle column. But using my code I am getting output
> like this
>
>
> pts/4
> pts/5
>
> Where I am making the mistake............ please rectify.....
>
> Thanks & Regards in advance
> Anirban Adhikary

Anirban,

The output of 'w' is delimited by whitespace, not necessarily a single space. 
Try passing the pattern '\w+' to split. Something like this:

#!/usr/bin/perl

use strict;
use warnings;

open (LS, "w|") or die "can't open w: $!";

<LS>;
<LS>;

while (<LS>) {
        print ((split)[4], "\n");
}

Stephen Kratzer
Network Engineer
CTI Networks, Inc.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to