免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1774 | 回复: 2
打印 上一主题 下一主题

求助:一个关于替换操作符的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-07-12 14:58 |只看该作者 |倒序浏览
CODE:
------------------
#!/usr/local/bin/perl

open(WOUT,"w|";
$time = <WOUT>;
$time =~s/^ *//;
$time =~s/ .*//;
close(WOUT);
print("Current timetime";
--------------------
w命令列出当前时间、系统负载和登录的用户,以及每个用户的作业时间和当前运行的命令,如:
  4:25pm  up 1 day,  6:37,  6 users,  load average: 0.79, 0.36, 0.28
其中的$time =~s/ .*//;删去除时间和结尾换行符之间的所有字符,存入变量$time。
我不知道时间段怎么保留的,麻烦指点下,想老久了。谢谢!!




------
万事开头难啊

论坛徽章:
0
2 [报告]
发表于 2006-07-12 16:25 |只看该作者

回复 1楼 wellshunan 的帖子

Hallo,

before processing output's data from command "w", you should study the STDOUT from "w":
i.e: under my linuxbox:

  1. shell> w
  2.   10:04:56 up 22 days, 23:25, 15 users,  load average: 0,05, 0,08, 0,02
  3. USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
  4. foo    :0       -                19Jun06 ?xdm?  24:29m  9.14s /usr/bin/gnome-session
  5. foo    pts/0    :0.0             19Jun06  4days  0.06s  0.06s bash
  6. ...
复制代码


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:

  1. open WOUT, "w|";
  2. @w = <WOUT>;
  3. close WOUT;

  4. $w_summary = $w[0];  # to process 1. line
  5. $w_field_title = $w[1];  # to process 2. line

  6. # fetch w's records started fron third. line ...
  7. foreach my $line (@w[2..$#w]) {
  8.     # must set total colmns in spilt exactly 8
  9.     ($USER, $TTY, $FROM, $LOGIN, $IDLE, $JCPU, $PCPU, $WHAT) = split(/\s+/, $line, 8);
  10.     # processing field: i.e.:
  11.     # your homework
  12.     print "DEBUG; $LOGIN, $WHAT";}';
  13. }

复制代码


Best,
    ulmer

--------
Just 4 Fun

论坛徽章:
0
3 [报告]
发表于 2006-07-12 17:16 |只看该作者
Thank you!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP