hi i’m struggling to figure out how to get the top 5 lines of the PID and COMMAND headers when opening top
at the moment im using
top | awk '{print $1,$2}NR==5{exit}'
this is obviously annoyingly giving me this:
Processes: 390
2021/12/17 13:47:48
Load Avg:
CPU usage:
SharedLibs: 146M
>Solution :
With your shown samples, please try following code. Since you need top 5 PIDs details so printing first 5 lines wouldn’t work here. So skipping first 6 lines in output of top(which are about system details).
top -b | awk 'FNR>=7 && FNR<=12{print $1};FNR==12{exit}'
Explanation: Simple explanation of above code would be, passing top command’s output to awk as an standard input. Then in awk program checking condition if line number is from 7th to 12th then print it and on 12th line exit out of program.
Where definition of top -b option is as follows from man top:
b :Batch-mode operation Starts top in Batch mode, which could be
useful for sending output from top to other programs or to a file. In
this mode, top will not accept input and runs until the iterations
limit you’ve set with the `-n’ command-line option or until killed.