Parse ps' “etime” output and convert it into seconds

后端 未结 14 1941
难免孤独
难免孤独 2020-12-30 12:43

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 se

14条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-30 12:57

    Works on AIX 7.1:

    ps -eo etime,pid,comm | awk '{if (NR==1) {print "-1 ",$0} else {str=$1; sub(/-/, ":", str="0:0:"str); n=split(str,f,":"); print 86400*f[n-3]+3600*f[n-2]+60*f[n-1]+f[n]," ",$0}}' | sort -k1n
    

提交回复
热议问题