解析ps的'" ETIME"输出,并将其转换成秒 [英] Parse ps' "etime" output and convert it into seconds

查看:239
本文介绍了解析ps的'" ETIME"输出,并将其转换成秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些是可能的输出格式为 PS ^ h -eo ETIME

These are possible output formats for ps h -eo etime

21-18:26:30
   15:28:37
      48:14
      00:01

如何把它解析为秒?

How to parse them into seconds?


  • 请假设至少3位数的日子一部分,我不知道它可以有多长。

  • 输出为 egreped 一个只有一行所以没有必要为一个循环。

  • Please assume at least 3 digits for the days part as I don't know how long it can be.
  • The output will be egreped to one only line so no need for a loop.

推荐答案

通过awk的:

#!/usr/bin/awk -f  
BEGIN { FS = ":" }
{
  if (NF == 2) {
    print $1*60 + $2
  } else if (NF == 3) {
    split($1, a, "-");
    if (a[2] != "" ) {
      print ((a[1]*24+a[2])*60 + $2) * 60 + $3;
    } else {
      print ($1*60 + $2) * 60 + $3;
    }
  }
}

与运行:

awk -f script.awk datafile

输出:

1880790
55717
2894
1

最后,如果你想管解析器,你可以做这样的事情:

And finally, if you want to pipe to the parser, you can do something like this:

ps h -eo etime | ./script.awk

这篇关于解析ps的'" ETIME"输出,并将其转换成秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆