linuxcommandtop-command

How to get %CPU and %MEM only from top command


I am looking for the command/filter for the top command to get only %CPU and %MEM columns. Currently I am using like :

Command: top -b -p 1665,2398 -n 1 -o -PID | tail -n +7

Output:

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

1665 user 20 0 40.4g 2.3g 39956 S 0.0 2.6 0:26.67 java

2398 user 20 0 5630552 271860 17552 S 0.0 0.3 0:04.33 java

Expected output:

PID %CPU %MEM
1665 0.0 2.6
2398 0.0 0.3

Please give some idea. thank you


Solution

  • you may try with Awk. top -b -p 4225,5290 -n 1 |grep "^ " | awk '{ printf("%-8s %-8s %-8s\n", $1, $9, $10); }'

    PID %CPU %MEM

    4225 43.8 2.1

    5290 0.0 2.1