Linux sort Command


Introduction

The sort command in Linux is a powerful and versatile tool that is used to sort lines of text files in a variety of ways. It can be used to sort files alphabetically, numerically, or in reverse order. It also has the ability to sort based on specific fields within a file, making it a valuable tool for data analysis and manipulation. In this article, we will explore the different options and usage of the sort command.

Basic use of sort command

The basic syntax for the sort command is pretty simply as follows −

$ sort [options] [file...]

where options are the additional options you want to use and file is the name of the file you want to sort. If no file is specified, the sort will be read from standard input. For example, to sort the contents of a file named "file.txt" alphabetically, use the following command −

$ sort file.txt
apple
banana
cherry
date
elderberry
fig

Sorting numerically with sort command

The sort command also has the ability to sort numbers numerically. To do this, use the -n option. For example, to sort the contents of a file named "numbers.txt" numerically, use the following command −

$ sort -n numbers.txt
1
2
3
4
5
6

Sorting by field with sort command

The sort command also has the ability to sort by specific fields within a file. This is done using the -k option, followed by the field number. For example, to sort the contents of a file named "file2.txt" by the second field, use the following command −

$ sort -k 2 file2.txt
apple   1
banana  2
cherry  3
date	4
elderberry 5
fig 6

Sorting files & directories

The sort command can also be used to sort the contents of a directory. To do this, use the -f option to ignore case, the -r option to reverse the order, and the -t option to specify the delimiter. For example, to sort the contents of a directory named "dir" and display the results in reverse order, use the following command −

$ ls -l dir | sort -f -r -t ' '
drwxrwxr-x  2 user  group  4096 Jan 01 12:00 dir2
-rw-rw-r--  1 user  group  1234 Jan 01 12:00 file1.txt
-rw-rw-r--  1 user  group  5678 Jan 01 12:00 file2.txt
-rw-rw-r--  1 user  group  9012 Jan 01 12:00 file3.txt
-rw-rw-r--  1 user  group  3456 Jan 01 12:00 file4.txt

Sorting with Multiple Keys

The sort command also has the ability to sort by multiple keys. This is done by using the -k option multiple times. For example, to sort the contents of a file named "file3.txt" first by the second field and then by the third, use the following command −

$ sort -k 2,3 file3.txt
apple   1   A
banana  2   B
cherry  3   C
date	4   D
elderberry 5 E
fig 6 F

Removing Duplicates with sort command

The sort command also has the ability to remove duplicate lines from a file. To do this, use the -u option. For example, to remove duplicate lines from a file named "file4.txt", use the following command −

$ sort -u file4.txt
apple
banana
cherry
date
elderberry
fig

Advanced use and additional options

The sort command in Linux offers a variety of advanced options and usage scenarios beyond the basic sorting capabilities. These additional options can be used to fine-tune the sorting process and tailor it to specific needs.

Specifying the Collation Order

The sort command allows for specifying the collation order, which is the order in which characters are compared and sorted. This can be done using the LC_COLLATE environment variable. For example, to sort a file called "file5.txt" in reverse order, you would use the following command −

$ LC_COLLATE=C sort -r file5.txt
elderberry
date
cherry
banana
apple

Ignoring Leading Characters

Another advanced option is the ability to ignore leading characters when sorting. This can be done using the -b option. For example, to sort a file called "file6.txt" and ignore leading whitespace, you would use the following command −

$ sort -b file6.txt
apple
banana
cherry
date
elderberry

Conclusion

The sort command in Linux is a powerful and versatile tool that can be used to sort files in a variety of ways. You can sort alphabetically, numerically, and by specific fields within a file. It can also be used to sort directories and remove duplicate rows. With the variety of options available, the sort command can be an invaluable tool for analyzing and manipulating data.

Updated on: 13-Feb-2023

450 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements