Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until 2nd December, 2025:
Sorting by Two Fields in Linux
Last updated: September 26, 2024
1. Overview
Sorting data is common in Linux, especially with text files containing multiple columns or fields. Sorting by a single field is straightforward. However, sorting by numerous fields requires understanding the nuances of the sort command and its key specifications.
In this tutorial, we’ll explore the process of sorting by two fields in Linux. Additionally, we’ll demonstrate practical examples and address common pitfalls that users encounter.
2. Basic Sorting with the sort Command
We use the sort command in Linux to organize text files based on specified criteria. By default, it sorts the entire line alphabetically. Nevertheless, we can control which part of the line to sort with the use of the -k option (key).
Additionally, to sort more than one field, we need to specify the key for each field in the order we want to sort. Here is the basic syntax of the sort command:
$ sort [options] [file]
Let’s now understand what each of the components of the sort command means:
- [options]: these are the various flags or switches that affect the behavior of the command
- [file]: this is the file to be sorted. We can pipe input from another command or redirect output
For illustration, let’s create a text file called people.txt with the following contents:
Simon Strange 62
Pete Brown 37
Mark Brown 46
Stefan Heinz 52
Tony Bedford 50
John Strange 51
Fred Bloggs 22
James Bedford 21
Emily Bedford 18
Ana Villamor 44
Alice Villamor 50
Francis Chepstow 56
Now, let’s do a basic sorting of the file people.txt by the second field:
$ sort -k2,2 people.txt
Emily Bedford 18
James Bedford 21
Tony Bedford 50
Fred Bloggs 22
Mark Brown 46
Pete Brown 37
Francis Chepstow 56
Stefan Heinz 52
...
The command sorts the file by the second field. In the output, the file is sorted alphabetically based on the second field.
3. Sorting by Two Fields
When sorting by multiple fields, we need to define each field separately using the -k option and specify both the start and stop positions of the field. In particular, this is important to avoid issues where the sort command considers parts of the line we don’t want it to.
For example, let’s continue with the people.txt file and explore how to sort it by two fields. First, we sort by last name (the second field) and then by first name (the first field):
$ sort -k2,2 -k1,1 people.txt
Emily Bedford 18
James Bedford 21
Tony Bedford 50
Fred Bloggs 22
Mark Brown 46
Pete Brown 37
Francis Chepstow 56
...
In this case, the -k2, 2 sorts the data by the second field (last name). Additionally, the -k1,1 further sorts the data by the first field (first name) if the two last names are the same.
Furthermore, In the output, the file is sorted first by last name and then by first name when two last names are the same. This makes the data more structured and easy to interpret.
4. Understanding Key Specifications
To ensure we sort accurately, we should understand how key specifications work when using the -k option. Additionally, When sorting by a specific field, we need to use both the start and stop positions to precisely control the part of the line we want to sort.
Let’s break down the key specifications:
- -k2,2: this constructs the sort command to use the second sorting, starting and stopping at the same field (field 2, which is the last name)
- -k1,1: similarly, this tells the sort command to only use the first field (first name) without considering any other parts of the line
Now, without specifying the stop position, the sort may include the remaining part of the line in its sort criteria, which can lead to unexpected results.
5. Addressing Common Pitfalls in Sorting by Multiple Fields
There are several common pitfalls when sorting by multiple fields. One of the most common issues occurs when we forget to specify both the start and stop positions for the key:
$ sort -k2 people.txt
Emily Bedford 18
James Bedford 21
Tony Bedford 50
Fred Bloggs 22
Pete Brown 37
Mark Brown 46
...
This command sorts the file based on the second field, but it considers everything after the second field including the age. As a result, the sorting may not behave as expected. In the output, the last name is sorted alphabetically, but the third field also influences the sorting.
To avoid this, we specify the stop position:
$ sort -k2,2 -k1,1 people.txt
Emily Bedford 18
James Bedford 21
Tony Bedford 50
Fred Bloggs 22
Mark Brown 46
Pete Brown 37
...
Now, the sorting is done purely based on the second and first fields.
6. Combining Alphabetical and Numeric Sorting
Let’s now combine both alphabetical and numeric sorting. For example, we sort the file by last name (alphabetically) and then by age (numerically):
$ sort -k2,2 -k3,3n people.txt
Emily Bedford 18
James Bedford 21
Tony Bedford 50
Fred Bloggs 22
Pete Brown 37
Mark Brown 46
Francis Chepstow 56
Stefan Heinz 52
John Strange 51
Simon Strange 62
Ana Villamor 44
Alice Villamor 50
In this command -k2,2 sorts the content of the people.txt file by the last name (alphabetically). Also, -k3,3n sorts the content of the people.txt file by the third field (age) numerically.
In the output, the data is properly sorted by last name followed by age.
7. Sorting in Reverse Order
We can also sort in reverse order using the -r option. For example, let’s sort by age in descending order:
$ sort -k3,3nr people.txt
Simon Strange 62
Francis Chepstow 56
Stefan Heinz 52
John Strange 51
Alice Villamor 50
Tony Bedford 50
Mark Brown 46
Ana Villamor 44
...
This command sorts the third field in reversed other.
8. Conclusion
In this article, we’ve explored how to sort data in Linux using the sort command, focusing on sorting by multiple fields. We’ve covered basic sorting, sorting by two fields, and addressed common pitfalls. Additionally, we combined alphabetical and numeric sorting and demonstrated how to reverse the sort order.
We can efficiently organize and manipulate data in text files, making complex sorting tasks easier and more reliable by mastering these techniques.