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

2020-02-26 10:32发布

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?

  • 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.

14条回答
手持菜刀,她持情操
2楼-- · 2020-02-26 10:45

I've implemented a 100% bash solution as follows:

#!/usr/bin/env bash

etime_to_seconds() {
  local time_string="$1"
  local time_string_array=()
  local time_seconds=0
  local return_status=0

  [[ -z "${time_string}" ]] && return 255

  # etime string returned by ps(1) consists one of three formats:
  #         31:24 (less than 1 hour)
  #      23:22:38 (less than 1 day)
  #   01-00:54:47 (more than 1 day)
  #

  # convert days component into just another element
  time_string="${time_string//-/:}"

  # split time_string into components separated by ':'
  time_string_array=( ${time_string//:/ } )

  # parse the array in reverse (smallest unit to largest)
  local _elem=""
  local _indx=1
  for(( i=${#time_string_array[@]}; i>0; i-- )); do
    _elem="${time_string_array[$i-1]}"
    # convert to base 10
    _elem=$(( 10#${_elem} ))
    case ${_indx} in
      1 )
        (( time_seconds+=${_elem} ))
        ;;
      2 )
        (( time_seconds+=${_elem}*60 ))
        ;;
      3 )
        (( time_seconds+=${_elem}*3600 ))
        ;;
      4 )
        (( time_seconds+=${_elem}*86400 ))
        ;;
    esac
    (( _indx++ ))
  done
  unset _indx
  unset _elem

  echo -n "$time_seconds"; return $return_status
}

main() {
  local time_string_array=( "31:24" "23:22:38" "06-00:15:30" "09:10" )

  for timeStr in "${time_string_array[@]}"; do

      local _secs="$(etime_to_seconds "$timeStr")"
      echo "           timeStr: "$timeStr""
      echo "  etime_to_seconds: ${_secs}"
  done

}

main
查看更多
▲ chillily
3楼-- · 2020-02-26 10:48

Here's a PHP alternative, readable and fully unit-tested:

//Convert the etime string $s (as returned by the `ps` command) into seconds
function parse_etime($s) {
    $m = array();
    preg_match("/^(([\d]+)-)?(([\d]+):)?([\d]+):([\d]+)$/", trim($s), $m); //Man page for `ps` says that the format for etime is [[dd-]hh:]mm:ss
    return
        $m[2]*86400+    //Days
        $m[4]*3600+     //Hours
        $m[5]*60+       //Minutes
        $m[6];          //Seconds
}
查看更多
The star\"
4楼-- · 2020-02-26 10:48

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
查看更多
Animai°情兽
5楼-- · 2020-02-26 10:50
[[ $(ps -o etime= REPLACE_ME_WITH_PID) =~ ((.*)-)?((.*):)?(.*):(.*) ]]
printf "%d\n" $((10#${BASH_REMATCH[2]} * 60 * 60 * 24 + 10#${BASH_REMATCH[4]} * 60 * 60 + 10#${BASH_REMATCH[5]} * 60 + 10#${BASH_REMATCH[6]}))

Pure BASH. Requires BASH 2+ (?) for the BASH_REMATCH variable. The regex matches any of the inputs and places the matched strings into the array BASH_REMATCH, which parts of are used to compute number of seconds.

查看更多
相关推荐>>
6楼-- · 2020-02-26 10:51

Think I might be missing the point here but the simplest way of doing this is:

ps h -eo etimes

Note the 's' on the end of etime.

查看更多
你好瞎i
7楼-- · 2020-02-26 10:54
#!/bin/bash
echo $1 | sed 's/-/:/g' |  awk -F $':' -f <(cat - <<-'EOF'
  {
    if (NF == 1) {
        print $1
    }
    if (NF == 2) {
        print $1*60 + $2
    }
    if (NF == 3) {
        print $1*60*60 + $2*60 + $3;
    }
    if (NF == 4) {
        print $1*24*60*60 + $2*60*60 + $3*60 + $4;
    }
    if (NF > 4 ) {
        print "Cannot convert datatime to seconds"
        exit 2
    }
  }
EOF
) < /dev/stdin

Then to run use:

ps -eo etime | ./script.sh 
查看更多
登录 后发表回答