I would like to capture the output of the top command to use in another program however I only need certain information, more precisely I only need the USER, PID, CPU, COMMAND columns.
I already have the command top -b -n 1 | sed -n '7,12p'
to filter the top 5 results but I cannot go any further because I do not know much about sed/awk.
Example: here is what I get
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
4 root 20 0 98748 50608 4608 S 6.4 4.9 212:12.16 X
1 root 20 0 2132 128 96 S 0.0 0.0 0:07.62 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 7:28.54 ksoftirqd/0
and here is what i want
PID USER %CPU COMMAND
4 root 6.4 X
1 root 0.0 init
2 root 0.0 kthreadd
3 root 0.0 ksoftirqd/0
To filter the top output to a specific process, press the O key and enter the entry as COMMAND=name, where the name refers to the process name. Press ENTER, and the top utility will filter the processes to systemd only. You can also highlight the specific process while keeping other processes in view.
SHR (Shared Memory Size in KiB): Stands for a subset of resident memory (RES) that may be used by other processes. %CPU (CPU Usage): Stands for the task's share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time.
pass to:
awk '{print $1,$2,$9,$NF}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With