Skip to content

Instantly share code, notes, and snippets.

@GitarPlayer
Last active May 10, 2022 19:31
Show Gist options
  • Save GitarPlayer/f34c42957fbd07f71da0c348dbb47d3c to your computer and use it in GitHub Desktop.
Save GitarPlayer/f34c42957fbd07f71da0c348dbb47d3c to your computer and use it in GitHub Desktop.

Sorting cmd output by column with the sort command

Some commands come with their own sorting functionality. Anyhow it's an useful flag to have in your repertoire

So let's say we want to sort by VSZ and we have this output.

[azureuser@alma ~]$ ps aux |head -5
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  1.5  0.1 175056 13480 ?        Ss   19:24   0:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 16
root           2  0.0  0.0      0     0 ?        S    19:24   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        I<   19:24   0:00 [rcu_gp]
root           4  0.0  0.0      0     0 ?        I<   19:24   0:00 [rcu_par_gp]

We simply add a pipe with sort -k 5:

[azureuser@alma ~]$ ps aux |sort -k 5 -nr |head -5
polkitd     1002  0.0  0.3 1628196 27504 ?       Ssl  19:24   0:00 /usr/lib/polkit-1/polkitd --no-debug
root        1073  0.1  0.3 494096 31760 ?        Ssl  19:24   0:00 /usr/libexec/platform-python -Es /usr/sbin/tuned -l -P
root        1335  0.2  0.3 447296 30840 ?        Sl   19:24   0:00 /usr/bin/python3.6 -u bin/WALinuxAgent-2.7.1.0-py2.7.egg -run-exthandlers
root         997  0.0  0.1 411244  8488 ?        Ssl  19:24   0:00 /usr/libexec/stratisd --log-level debug
root        1069  0.0  0.2 389960 18508 ?        Ssl  19:24   0:00 /usr/sbin/NetworkManager --no-daemon

and we see the 5 processes that use the most VSZ.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment