Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting the first 5 lines from PID and COMMAND using top

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}'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading