Parsing a file name with awk

I have a command to print out the top 5 most cpu intensive commands:

ps aux --sort=-%cpu --no-headers

I would like to make a printout containing only the parsed file name.

So, I want to turn this:

$ ps aux --sort=-%cpu --no-headers
user     24883  4.3  1.7 2010436 131440 ?      Sl   17:07   0:06 /usr/lib/x86_64-linux-gnu/qt5/libexec/QtWebEnginePr
user     25475  1.9  2.4 2048140 182668 ?      Sl   16:25   0:52 /usr/lib/x86_64-linux-gnu/qt5/libexec/QtWebEnginePr
user     20794  1.7  3.6 3393352 277388 ?      Sl   16:22   0:49 falkon
user       541  0.3  0.5 433640 38748 tty1     Sl   10:45   1:18 /usr/lib/xorg/Xorg -nolisten tcp :0 vt1 -keeptty -a
user     18867  0.2  0.0   4396  1020 ?        S    15:40   0:11 /bin/sh /home/user/scripts/./status.sh
root         8  0.1  0.0      0     0 ?        I    10:45   0:23 [rcu_sched]
root         1  0.0  0.1 216728  8760 ?        Ss   10:45   0:07 /sbin/init
root         2  0.0  0.0      0     0 ?        S    10:45   0:00 [kthreadd]
root         4  0.0  0.0      0     0 ?        I<   10:45   0:00 [kworker/0:0H]
root         6  0.0  0.0      0     0 ?        I<   10:45   0:00 [mm_percpu_wq]
root         7  0.0  0.0      0     0 ?        S    10:45   0:00 [ksoftirqd/0]
root         9  0.0  0.0      0     0 ?        I    10:45   0:00 [rcu_bh]
root        10  0.0  0.0      0     0 ?        S    10:45   0:00 [migration/0]
root        11  0.0  0.0      0     0 ?        S    10:45   0:00 [watchdog/0]
root        12  0.0  0.0      0     0 ?        S    10:45   0:00 [cpuhp/0]
...

Into this:

QtWebEnginePr
QtWebEnginePr
falkon
Xorg
sh

Note that '/usr/lib/xorg/Xorg -nolisten tcp :0 vt1 -keeptty -a' has been trimmed to say only 'Xorg'; I would like the file name itself with no command parameters at the end.

So far I have this:

$ ps aux --sort=-%cpu --no-headers | awk 'NR==1,NR==5{print $11}'
/usr/lib/x86_64-linux-gnu/qt5/libexec/QtWebEngineProcess
falkon
/usr/lib/x86_64-linux-gnu/qt5/libexec/QtWebEngineProcess
/usr/lib/xorg/Xorg
/bin/sh

I understand that I could just slap a cut command on the end of it and it's all fine:
ex.,

ps aux --sort=-%cpu --no-headers | awk 'NR==1,NR==5{print $11}' | cut -d "/" -f2)

but I don't want to use anymore commands to complete this task; this command will be repeated every 1 second or so and printed out in my status bar, so I want it to be efficient. And I know that awk is powerful enough to do this; but I just don't seem to be able to do this all in one. I could parse the beginning of the filename but the end out be out of control. And vise-versa. I have a feeling it's something to do with '-F/' but how to incorporate the two?

Any ideas?

I'm using sh shell and running Debian 9.

wouldn't this suffice (or a variation of):

ps -axeo comm --no-header --sort=-%cpu
1 Like

Indeed it would! This is perfect! :slight_smile: I'm guessing that this is far more efficient than using awk.

I just added a 'head' command to chop off the rest of the lines after 5:

ps axeo comm --sort=-%cpu --no-header | head -5

SOLVED!!! :smiley: