Ps command and awk - shorten characters

ps -e -o pcpu -o pid -o user -o args | sort -k 1 | tail -6r

 %CPU     PID     USER COMMAND
  0.3  223220     root /usr/tivoli/tsm/client/ba/bin/dsmc sched 
  0.2  411332     root /usr/sbin/syslogd 
  0.1   90962     root /usr/sbin/syncd 60 
  0.0 10572  root -ksh 
  0.0  94270  root -ksh 

in the above, the COMMAND column can contain process names or syntaxes that are extremely long. is there a one liner awk command i can use with the above ps command that will limit the number of characters, specifically those under the COMMAND column? I'd like to limit them to just 60 characters.

OS:
AIX

Shell:
sh

man fold
.... | cut -c 1-60

You may try something like this,

awk '{x = index($0,$4);$0 = substr($0,1,x-1) substr($0,x,60)}1'
ps -e -o pcpu -o pid -o user -o args | sort -k 1 | tail -6r | awk '{x = index($0,$4);$0 = substr($0,1,x-1) substr($0,x,60)}1'
1 Like

Or use comm instead that will give simple name of executable:

ps -eo pcpu,pid,comm

You can further simplify that to:

awk '{x = index($0,$4);print substr($0,1,x+60)}'
2 Likes