The sort command in Linux sorts lines of input files based on sorting criteria. This command is quite useful for many sysadmins who handle text files daily. In addition to this, the sort command is very flexible and sorts efficiently according to your needs.
One of the advantages of sort is that since it is part of the GNU Core Utils you can use it natively in the system. That is to say, that you do not have to install or to configure something extra.
Like many other commands, the sort command has options that modify its behavior. In this post, we will help you to use them according to the circumstances with examples.
Let’s go for it.
sort syntax
The basic syntax of the sort
command is as follows
sort [option] [file]
or
sort [OPTION]... --files0-from=F
Below are some of common options supported by this command, you can refer man sort page for more details.
- b – ignore leading blanks
- d – dictionary-order (consider only blanks and alphanumeric characters)
- f – ignore case while sorting
- g -general-numeric-sort (compare according to general numerical value)
- i – ignore-nonprinting consider only printable characters
- M – month-sort compare (unknown) < ‘JAN’ < … < ‘DEC’
- h – human-numeric-sort compare human readable numbers (e.g., 2K 1G)
- n – numeric-sort compare according to string numerical value
- R – random-sort shuffle, but group identical keys. –random-source=FILE get random bytes from FILE
- r – reverse reverse the result of comparisons
Using sort command
The basic use of sort command is to sort the file. Let’s consider a file example.txt
whose contents are as follows
Motor
Scar
Car
Op
Now, simply run sort command with file name as input.
sort example.txt
You gets an output screen like the following one
Car
Motor
Op
Scar
To reverse the order, use the -r
option
sort -r example.txt
This are basic uses. The Sort command support differnet options which change the behavour of the command. Let’s try to cover some common option with examples below.
Sort Command Examples
As I always say, upper and lower case can be a problem in text files. To make the sort ignore them, just use the -f
option.
sort -f example.txt
Another useful option is the -u
option, which allows us to check if the file is already sorted.
sort -u example.txt
If you do not get any output on the screen, it is already sorted.
If you want to sort and at the same time remove duplicates, you can use the -u
option.
sort -u example.txt
Using the -k
option and a number, you can specify which field is to be referenced.
For example, let’s start from a file named example2.txt
that has the following content
1010 Aaron Trunk
1598 Zac Eron
3578 Fabian mo
We can set it to be sorted by name. That is to say, by the second field of the text line.
sort -k 2 example2.txt
Output:
1010 Aaron Trunk
3578 Fabian mo
1598 Zac Eron
As you can see, it is basic to use the sort
command. If you are going to sort numbers, then use the -n
option.
sort -n file.txt
You can combine it with the -r
option.
sort -nr file.txt
Or if you have a file with months, the right option is -M
.
sort -M file.txt
So enjoy it.
Conclusion
In this post, you have learned how to use the sort command to sort text files. Quite useful in configurations and especially for manipulating this kind of files via the terminal.