Open In App

How to sort lines in text files in Linux | sort Command

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

SORT command is used to sort a file, arranging the records in a particular order. By default, the sort command sorts file assuming the contents are ASCII. Using options in the sort command can also be used to sort numerically. 

  • SORT command sorts the contents of a text file, line by line.
  • sort is a standard command-line program that prints the lines of its input or concatenation of all files listed in its argument list in sorted order.
  • The sort command is a command-line utility for sorting lines of text files. It supports sorting alphabetically, in reverse order, by number, by month, and can also remove duplicates.
  • The sort command can also sort by items not at the beginning of the line, ignore case sensitivity, and return whether a file is sorted or not. Sorting is done based on one or more sort keys extracted from each line of input.
  • By default, the entire input is taken as the sort key. Blank space is the default field separator.

The sort command follows these features as stated below:  

  1. Lines starting with a number will appear before lines starting with a letter.
  2. Lines starting with a letter that appears earlier in the alphabet will appear before lines starting with a letter that appears later in the alphabet.
  3. Lines starting with a uppercase letter will appear before lines starting with the same letter in lowercase.

Syntax of sort Command in Linux

Here,

options‘ refer to the various flags and parameters that can be used to customize the sorting behavior, and ‘file’ is the name of the file to be sorted.

If no file is specified, ‘sort‘ reads from the standard input.

Options Available in Sort Command

Options

Description

-o

Specifies an output file for the sorted data. Functionally equivalent to redirecting output to a file.

-r

Sorts data in reverse order (descending).

-n

Sorts a file numerically (interprets data as numbers).

-nr

Sorts a file with numeric data in reverse order. Combines -n and -r options.

-k

Sorts a table based on a specific column number.

-c

Checks if the file is already sorted and reports any disorder.

-u

Sorts and removes duplicate lines, providing a unique sorted list.

-M

Sorts by month names.

How to Sort Lines in Text Files in Linux Using sort Command

Let’s start with a simple example. Consider a file named ‘file.txt’ with the following content:

cat file.txt
Displaying content of file using cat command

Displaying content of file using cat command

Here we used cat command to display the content inside the file name ‘file.txt’.

To sort the lines alphabetically, you can use the following command:

sort file.txt
sort lines in text using sort command

sort lines in text using sort command

Note: This command does not actually change the input file, i.e. file.txt . We can verify this using cat command .

no change is done to real file

no change is done to real file

How to Sort Lines in Text Files with Uppercase and Lowercase Using sort Command

Sort function with mix file i.e. uppercase and lower case: When we have a mix file with both uppercase and lowercase letters then first the upper case letters would be sorted following with the lower case letters.

Example: If we have a text file which has both Uppercase andlowercase characters.

cat mix.txt
Displaying content of file using cat command

Displaying content of file using cat command

Here we used cat command to display the content inside the file name ‘mix.txt’.

To sort the lines alphabetically which contain uppercase and lowercase letters, you can use the following command:

sort mix.txt
sort uppercase and lowercase lines in text using sort command

sort uppercase and lowercase lines in text using sort command

How to do Numeric Sorting in Lines in Text Files Using sort Command

By default, ‘sort’ treats numbers as text and sorts them accordingly. If you want to perform numeric sorting, use the ‘-n’ option:

cat file1.txt
Displaying content of file using cat command

Displaying content of file using cat command

Here we used cat command to display the content inside the file name ‘file1.txt’.

Now we will sort the numeric data using sort command as follows.

sort file1.txt
sorting numerical data in linux file

sorting numerical data in linux file

How to Sort Lines in Reverse Order in Linux File Using sort Command

To sort in reverse order, you can use the ‘-r’ option:

Example: If we want to sort lines in reverse order in Linux file we can use sort command with -r option, suppose we have a file name “example.txt” as shown below.

cat example.txt
using cat command to display content inside file

using cat command to display content inside file

Now sorting lines in reverse order using `-r` option in sort command

sort -r example.txt
sorting lines in reverse order using -r with sort command

sorting lines in reverse order using -r with sort command

How to sort Specific Fileds in Linux Files Using sort Command

Imagine you have a file named ’employee_data.txt’ that contains information about employees in a tab-separated format. Each line represents a record with details such as employee ID, name, department, and salary, separated by tabs. You want to organize this data based on the department and display the results in alphabetical order.

For Example: If our file name is “employee_data.txt” and content inside it is as follows.

cat employee_data.txt
Displaying content of file using cat command

Displaying content of file using cat command

To achieve this, you can use the ‘sort’ command

sort -k3 employee_data.txt

Explanation:

-k3: This option indicates that the sorting should be done based on the third column (Department).

sorting a particular column using sort command

sorting a particular column using sort command

In this example, the employee records are now sorted alphabetically based on the ‘Department’ column. The ‘sort’ command, with the custom delimiter, allows you to efficiently organize and analyze tab-separated data, making it a valuable tool for managing structured information in various scenarios.

Application and uses of sort command:

  1. It can sort any type of file be it table file text file numeric file and so on.
  2. Sorting can be directly implemented from one file to another without the present work being hampered.
  3. Sorting of table files on the basis of columns has been made way simpler and easier.
  4. So many options are available for sorting in all possible ways.
  5. The most beneficial use is that a particular data file can be used many times as no change is made in the input file provided.
  6. Original data is always safe and not hampered.

Frequently Asked Question on Sort Command

Can the ‘sort’ command be used to sort files in descending order by default?

No, the ‘sort’ command sorts files in ascending order by default. To achieve descending order, the ‘-r’ option should be used.

How can I sort a file based on a specific column using the ‘sort’ command?

Use the ‘-k’ option followed by the column number to sort a file based on a specific column.

For example,

sort -k 2 data.txt 

This will sort based on the second column.

What is the difference between using the ‘-o’ option and redirecting output to a file when using the ‘sort’ command?

Both methods achieve the same result of saving the sorted output to a file. The ‘-o’ option allows specifying the output file directly in the command, while redirection (e.g., `sort file.txt > output.txt`) achieves the same result.

How can I check if a file is already sorted using the ‘sort’ command?

Use the ‘-c’ option with the ‘sort’ command (e.g., sort -c file.txt). If the file is already sorted, there will be no output. If there are disorderly lines, they will be reported.

Is it possible to sort a file with numeric data in reverse order using the ‘sort’ command?

Yes, the ‘-n’ option sorts files with numeric data, and the ‘-r’ option can be combined with it (e.g., sort -nr numbers.txt) to achieve a reverse numeric sort.

Conclusion

In this article we discussed the ‘sort’ command in Linux which is a powerful utility for arranging text files in various orders. It supports alphabetical, numerical, and reverse sorting, along with options for sorting specific columns and removing duplicates. The command is versatile, handling different file types, including tables and numeric data. The article provides a comprehensive overview of the ‘sort’ command, covering syntax, options, and practical examples. Frequently asked questions (FAQs) address common queries, making it a valuable resource for efficient data organization and analysis on the Linux command line.



Last Updated : 24 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads