Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


LPI Linux Essentials Exam 010-160 - Topic 3.2 - sort Command

The sort command in Linux is an incredibly flexible utility designed for arranging lines in text files according to various attributes. Whether you're a data scientist needing to sort numerical lists, a system administrator aiming to organize log entries, or a regular user looking to make sense of a text document, the sort command can be indispensable for your tasks. This guide aims to provide a comprehensive explanation of this utility, covering basic usage as well as advanced options.

Usage

The most straightforward way to use the sort command follows the below syntax:

sort [options] [file...]

Here, options represent the flags that modify the behavior of the command, and file specifies the file or files that you want to sort. If no file is specified, sort will read from the standard input.

Practice File

To follow along with the exercises in this guide, run the below to create a practice file:

Create practice file:

cat << EOF > /tmp/sort.txt
apple 5
Banana 9
apple 2
dog 3
Zebra 1
cat 7
elephant 6
Banana 4
apple 5
Frog 8
Giraffe 12
zebra 11
Horse 10
elephant 13
Apple 2
EOF

Alphabetical Sorting

By default, the sort command will arrange lines in a file alphabetically, with a lowercase version of a letter being before an uppercase version.

Exercise: Alphabetical Sorting:

sort /tmp/sort.txt

The output will look like:

apple 2
Apple 2
apple 5
apple 5
Banana 4
Banana 9
cat 7
dog 3
elephant 13
elephant 6
Frog 8
Giraffe 12
Horse 10
Zebra 1
zebra 11

Sorting by Column

Sometimes you might need to sort text based on a particular column in a table-like format. The -k option allows you to specify the column by which to sort.

Exercise: Sorting by Column

sort -k 2 /tmp/sort.txt

The output will look like:

Zebra 1
Horse 10
zebra 11
Giraffe 12
elephant 13
apple 2
Apple 2
dog 3
Banana 4
apple 5
apple 5
elephant 6
cat 7
Frog 8
Banana 9

Notice how the 2nd column which is numbers is not in tru numerical order but in lexical order. The next exercise will sort in numerical order.

Numerical Sorting

The -n option allows you to sort lines numerically, which can be highly useful when dealing with lists of numbers.

Exercise: Numerical Sorting

Use the -k option to ensure we are sorting on the second column which contains numbers.

sort -k 2 -n /tmp/sort.txt

The output will look like:

Zebra 1
apple 2
Apple 2
dog 3
Banana 4
apple 5
apple 5
elephant 6
cat 7
Frog 8
Banana 9
Horse 10
zebra 11
Giraffe 12
elephant 13

Reverse Sorting

If you wish to reverse the sorting order, you can use the -r option.

Exercise: Reverse Sorting

sort -r /tmp/sort.txt

The output will be the opposite of alphabetical sorting, like so:

zebra 11
Zebra 1
Horse 10
Giraffe 12
Frog 8
elephant 6
elephant 13
dog 3
cat 7
Banana 9
Banana 4
apple 5
apple 5
Apple 2
apple 2

Unique Sorting

The -u option helps in removing duplicate lines from the sorted output.

Exercise: Unique Sorting

sort -u /tmp/sort.txt

The output will contain no duplicates:

apple 2
Apple 2
apple 5
Banana 4
Banana 9
cat 7
dog 3
elephant 13
elephant 6
Frog 8
Giraffe 12
Horse 10
Zebra 1
zebra 11

Wrapping Up

Understanding the intricacies of the sort command can immensely aid your data manipulation and text analysis tasks. With options ranging from numerical and alphabetical sorting to more specialized column-based sorting, the sort command is a valuable addition to anyone who interacts with Linux systems. By mastering its various options, you'll greatly expand your ability to manipulate and understand text and numerical data.


Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.