Linux sort Command Examples

What is Linux sort?

sort is a very useful command line utility used to sort the lines of a file or input stream. sort can be used to sort input by entire lines, single columns, or different column ranges in a variety of ways.

Sorting by Entire Lines

The default behavior of the sort command is to sort by entire lines. Here is an example:

Sorting by Columns

It is possible to sort files/streams on a single column, multiple columns, or ranges of columns. To do this use the -k option to indicate these columns. The default delimiter used to identify columns is white space (blank characters). If you need to use a different column delimiter use the -t option.

Sorting by a Single Column

Sorting by single column requires the use of the -k option. You must also specify the start column and end column to sort by. When sorting by a single column, these numbers will be the same. Here is an example of sorting a CSV (comma delimited) file by the second column.

Sorting by Multiple Columns

Sorting by multiple columns is similar to sorting by a single column. To sort on a range of columns, simply specify the start and end columns in the column range to use for sorting. Here is an example of sorting a CSV on columns 2 through 4.

To sort on a non-contiguous set of columns, you must use the -k option multiple times. Here is an example of sorting by column 2 then column 6:

Sorting in Reverse

It is easy to sort in reverse (descending) order with the Linux sort command. Simply use the -r option. Here is an example of sorting by entire lines in reverse order:

Sorting Numerically

By default the sort command will order lines based on text value, which doesn’t always make sense. For example, if we had the following numbers in a file:

1
9
10

If we sort on text values based on text values we will get:

To sort numerically we need to use the -n option. Using this we will now get the correct numerical ordering:

Complex Sorting

There are many ways that one might want to sort a text file. Here is a more complex example of sorting a file first on column 1 in numerical reverse order, and then on columns 2-4 using the default text ordering.

Random Sorting

Occasionally you want to print/sort files in random order. To do this with sort simply use it -R option.

Sorting by Case

Depending on your system, the default sort behavior could be either to sort text using case-sensitive or case-insensitive comparisons. To make the default case-sensitive simply use export LC_ALL=C before executing your sort command. To sort case-insensitive use the -f option.

Sorting Multiple Files together

Often when working with data you will want the sorted output of multiple files. There are a couple of easy ways to do this. First you can pass multiple files on the command line to be sorted, or you can cat the content of multiple files and pipe the output to the sort command:

Check if Input is Already Sorted

Knowing if your data is sorted can be helpful when using other commands like uniq, and can save a lot of processing time. To check if a file is already sorted use the -c option.

Learning More about Linux Sort

To learn more about how to use the Linux sort command, simply view the man page from you terminal:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">