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

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

问题描述

这些是 ps h -eo etime

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

如何将它们解析为秒?

  • 请假设天数部分至少为 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天全站免登陆