Linux/Unix – Sorting Output with sort

It can also be useful to sort the content of a file or the output of a command. This can be helpful in bringing order to disorderly output. The following examples list the names of all RPM packages currently installed, grabs any with kernel in the name, and sorts the results in alphanumeric order (forward and reverse):

$ dpkg-query -l | grep kernel | sort

Sort in alphanumeric order

$ dpkg-query -l | grep kernel | sort -r

Sort in reverse alphanumeric order

The following command sorts processes based on descending memory usage (fourth field of ps output). The –k option specifies the key field to use for sorting. 4,4 indicates that the fourth field, and only the fourth field, is a key field.

$ ps auwx | sort –r –k 4,4

The following command line sorts loaded kernel modules in increasing size order. The n option tells sort to treat the second field as a number and not a string:

$ lsmod | sort -k 2,2n

Advertisements

About msotela

This blog is for anyone who wants to access the power of a Linux system as a systems administrator or user. You may be a Linux enthusiast, a Linux professional, or possibly a computer professional who is increasingly finding the Windows systems in your data center supplanted by Linux boxes.

Posted on March 16, 2009, in Unix/Linux. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: