-2

There is a command and its output:

ps aux --sort=-%cpu | head -2 | sort -r

root         1  0.0  0.0    904   520 ?        Sl   12:09   0:00 /init
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

How to print only the first line with awk? or only second lane like this:

root         1  0.0  0.0    904   520 ?        Sl   12:09   0:00 /init

or this:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
anubhava
  • 761,203
  • 64
  • 569
  • 643
bronix1
  • 3
  • 2
  • Does this answer your question? [Bash tool to get nth line from a file](https://stackoverflow.com/questions/6022384/bash-tool-to-get-nth-line-from-a-file) – lojza Aug 22 '22 at 12:19

1 Answers1

0
ps aux --sort=-%cpu | awk 'NR==1'

ps aux --sort=-%cpu | awk 'NR==2'

No need for head or sort.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185