- 论坛徽章:
- 0
|
回复 1楼 wellshunan 的帖子
Hallo,
before processing output's data from command "w", you should study the STDOUT from "w":
i.e: under my linuxbox:
- shell> w
- 10:04:56 up 22 days, 23:25, 15 users, load average: 0,05, 0,08, 0,02
- USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
- foo :0 - 19Jun06 ?xdm? 24:29m 9.14s /usr/bin/gnome-session
- foo pts/0 :0.0 19Jun06 4days 0.06s 0.06s bash
- ...
复制代码
Output from "w" without additional options:
Fisrt line is summary message
Second line is field's title (8 fields or columns)
Rest lines are records with "\s+" seperated.
Howto processing this output? => improving your code:
1. read filehandle <WOUT> into an array.
2. Loop this array and deal with each line
3. using split() to fetch each field per line.
4. then processing those field which you want furtherly.
Sample code:
- open WOUT, "w|";
- @w = <WOUT>;
- close WOUT;
- $w_summary = $w[0]; # to process 1. line
- $w_field_title = $w[1]; # to process 2. line
- # fetch w's records started fron third. line ...
- foreach my $line (@w[2..$#w]) {
- # must set total colmns in spilt exactly 8
- ($USER, $TTY, $FROM, $LOGIN, $IDLE, $JCPU, $PCPU, $WHAT) = split(/\s+/, $line, 8);
- # processing field: i.e.:
- # your homework
- print "DEBUG; $LOGIN, $WHAT";}';
- }
复制代码
Best,
ulmer
--------
Just 4 Fun |
|