This chapter is from the book
sort
The sort command can be use to sort text data. By default, it will break each line of data into fields, using whitespace as the default delimiter. It also sorts on the first field in the data by default, performing a dictionary sort:
[student@localhost ~]$ cat individuals.txt tom nick sue Tim [student@localhost ~]$ sort individuals.txt nick sue Tim tom
Important options include the following:
Option | Description |
---|---|
-f | Fold case (essentially case-insensitive). |
-h | Human-based numeric sort (for example, 2K is lower than 1G). |
-n | Numeric sort. |
-M | Month-based sort. |
-r | Used to reverse the sort order. |
-t | Used to change the field separator (for example, sort -t ":" file.txt). |
-u | Used to remove duplicate lines. |
To sort on a different field than the default, use the -k option. Here’s an example:
[student@localhost ~]$ more people.txt 1 tom 2 nick 3 sue 4 tim [student@localhost ~]$ sort -k 2 people.txt 2 nick 3 sue 4 tim 1 tom