In Linux, the
sort command is a text processing utility that arranges lines in
text and output files. It sorts lines of text alphabetically or numerically,
based on a field or character position, making it a versatile tool for managing
The general syntax for the
sort command is:
sort [OPTION]... [FILE]...
[OPTION] specifies how the
sort command should behave, while
denotes the files that need to be sorted. If no file is provided,
read from the standard input.
|Consider only blanks and alphanumeric characters.
|Perform case-insensitive sorting.
|Compare according to numerical value.
|Reverse the sort, i.e., highest to lowest.
|Output only the first of an equal run.
|Define a key field to sort by.
|Output file name.
|Display the help message and exit.
|Display the version information and exit.
Create a text file named
names.txt with the following content:
sort command to sort the lines alphabetically:
Create a file named
To sort the numbers, use the
sort -n numbers.txt
For a case-insensitive sort, use the
-f option. Assuming
sort -f names.txt
Sorting By Column
Create a file called
To sort the students by scores:
sort -k2 -n students.txt
To sort a file while eliminating duplicate lines, use the
-u option. Given
sort -u duplicates.txt
Combining Multiple Options
You can combine multiple options for a more complex sort. To sort
numerically by scores in descending order:
sort -k2 -n -r students.txt
sort command is a powerful text-processing utility for organizing lines in
text files based on various criteria like alphabetical order, numerical value,
and even complex key-based sorting. It is often used in combination with other
text processing commands like
cut to create powerful text
manipulation pipelines. Learning how to use
sort effectively can significantly
enhance your text processing skills in Linux.
What Can You Do Next 🙏😊
If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.