Using sort command in Linux


The sort command in Linux is used to sort the lines of a text file. By using different options with this command you can print the output in various forms. For example, you can print the alphanumeric content in increasing order or reverse it by using the -r option with the sort command.

If you provide multiple inputs at once it will concatenate the text and print it in sorted order. Today here in this article we will discuss the sort command with some examples.

How to use the sort command in Linux

The syntax of using the sort command in Linux is given below –

sort [option] input_files

You can see detailed options on its manual page.

How does the sort command works

A text file can contain alphabets, numbers, or special characters. So sort command has some rules to do sorting of a text file.

  • The lines starting with numbers appear before the lines that start with alphabets
  • The lines with the letter that appears earlier will appear before the lines that starting with the alphabet that appear later
  • Lines that start with a lowercase letter will appear before the lines that start with an uppercase letter

As said earlier you can change the output by using options with the sort command.

Sorting the content of a file

Let’s say we have a data file called testfile.txt which has some content in it. The content of this file can be displayed using the cat command

cat testfile.txt

To sort the content of this file, use the following command in your terminal –

sort testfile.txt

Now see the output of this command –

This command doesn’t alter the content of the original file instead it only prints the sorted content on standard output. Now if you want to save the sorted content to a separate file then use the following command.

sort testfile.txt >sortedfile.txt

or use –

sort -o testfile.txt sortedfile.txt

This will sort the content of  testfile.txt and write it to a separate file called sortedfile.txt.

Sorting file’s content in the reverse order

If you want to sort the content in reverse order then use the following command in your terminal –

sort -r testfile.txt

To save the reverse sorted content into a file, use –

sort -r testfile.txt > rsortedfile.txt

Sorting content using a specific column of the table

Suppose you have a text file that contains data in the form of a table. If you want to sort the data on the basis of a specific column. Then use the option -k with sort command, now use the following command to sort the content on the basis of 5 column –

sort -k5 filename.txt

Now suppose you want to sort the content on the basis of 7 column which contains only numerical data then use the option -nk instead of using -k only with sort command –

sort -nk7 filename.txt

Instead of providing input using a text file, you can supply the input to sort commands from the pipeline.

For example –

ls -l|sort -nk7

Now see the output –

Sorting the content of multiple files using a single command

If you have multiple files and you want to sort their content then use the command as it is given below –

sort file1.txt file2.txt

To save this result in a third file use –

sort file1.txt file2.txt > sortedfile.txt

You can find more about the sort command in your terminal by using –

man sort

Conclusion

This is how you can sort the content of a file in the Linux terminal. Now if you have a query then write us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.