sort

2024-10-23

Basic Usage: Sorting Lines Alphabetically

The simplest application of sort is to order lines of a text file alphabetically. Let’s consider a file named unsorted.txt with the following content:

banana
apple
orange
grape
kiwi

To sort this file alphabetically, use the following command:

sort unsorted.txt

This will output:

apple
banana
grape
kiwi
orange

The output is displayed on the terminal. To save the sorted output to a new file, redirect the output using >:

sort unsorted.txt > sorted.txt

Sorting Numerically

sort isn’t limited to alphabetical sorting. It can also handle numerical data efficiently. Consider a file numbers.txt:

10
2
5
1
8

To sort these numbers in ascending order, use the -n (numerical) option:

sort -n numbers.txt

Output:

1
2
5
8
10

For descending order, use the -r (reverse) option in conjunction with -n:

sort -nr numbers.txt

Output:

10
8
5
2
1

Sorting by Specific Fields

When dealing with data containing multiple fields separated by a delimiter (often whitespace or a comma), sort allows you to sort by a specific field. Let’s use a file data.txt:

apple 10
banana 5
orange 20
grape 15

To sort by the second field (numerical values), use the -k (key) option:

sort -k2n data.txt

Output:

banana 5
apple 10
grape 15
orange 20

The -k2n specifies that sorting should be based on the second field (-k2) and that it should be treated numerically (-n). You can specify a range of fields using a hyphen, and even specify a character position within a field using a starting and ending position. For example -k1.3,1.5 would sort by the first field, characters 3 to 5.

Handling Case Sensitivity

By default, sort is case-sensitive. To perform a case-insensitive sort, use the -f (fold case) option:

sort -f unsorted.txt

This treats uppercase and lowercase letters as equivalent during the sorting process.

Unique Lines with -u

To display only unique lines, removing duplicates, use the -u option:

sort -u unsorted.txt

This will output only one instance of each line, even if it appears multiple times in the original file.

Combining Options

The power of sort lies in its ability to combine options. You can chain multiple options together to achieve complex sorting tasks. For example, to sort numerically in reverse order and only keep unique lines:

sort -urn numbers.txt

More Advanced Usage: -t and -k for Delimited Files

The -t option allows you to specify a field separator, and -k can target specific fields within delimited data. Let’s assume a comma-separated file csv_data.txt:

apple,red,10
banana,yellow,5
orange,orange,20
grape,green,15

To sort by the second field (color):

sort -t, -k2 csv_data.txt

This uses a comma as the field separator (-t,) and sorts based on the second field (-k2). Adding -f would make it case insensitive.

These examples showcase the core functionality of the sort command. Experimenting with different options and combining them will allow you to effectively manage and analyze text data from the command line. Exploring the man sort page will further unveil its extensive capabilities.