Linux Command sort and 6 Options

The Linux command sort is an incredibly useful tool for sorting files or input data. This command allows you to sort text files or data based on a specific column, either in ascending or descending order. It also supports sorting by numbers, alphabet, time, and more. In this post, we will cover the basic usage of the sort command, explain several options, and demonstrate how you can apply it effectively in different situations.

Basic Usage of the Linux Command sort

The sort command is most commonly used to sort text data. It typically reads data line by line from a text file and sorts it before outputting the results. By default, it sorts the content of filename.txt in alphabetical order (dictionary order).

sort filename.txt
ShellScript

Let’s assume you have a file with the following content:

banana
apple
tomato
cherry
raspberry
Plaintext

When you run the command sort filename.txt, the result will be sorted in dictionary order as shown below.

Figure 1. Sorting text file contents with the Linux command sort
Figure 1. Sorting text file contents with the Linux command sort

Extending Sorting Methods Using Options

The sort command offers several options that allow for more complex sorting tasks beyond basic alphabetical sorting. Let’s take a look at some of the key options.

-n: Numeric Sorting

By default, sort sorts data in alphabetical order. However, when dealing with numeric data, you can use the -n option to sort in numerical order.

sort -n number_file.txt
ShellScript

For example, if the file contains the following:

12
2
29
30
8
83
Plaintext

Without the -n option, the numbers won’t be sorted correctly, as shown below.

Figure 2. Attempting to sort numbers without using the -n option in the Linux command sort
Figure 2. Attempting to sort numbers without using the -n option in the Linux command sort

By executing sort -n number_file.txt, the numbers will be sorted properly. So, to sort both text and numbers, you need to use the -n option.

Figure 3. Sorting numbers using the -n option with the Linux command sort
Figure 3. Sorting numbers using the -n option with the Linux command sort

-r: Reverse Sorting

The -r option is used to sort results in reverse (descending) order. This can be applied to both numbers and letters.

sort -r filename.txt
ShellScript

The command will reverse the dictionary order. To sort files with mixed numbers and text in reverse order, you can use the -n option in combination with -r.

sort -nr 숫자파일.txt
ShellScript

This will sort both numbers and text in reverse order. When numbers and letters are mixed, they are sorted with letters first, followed by numbers.

Figure 4. Sorting in reverse order with mixed numbers and letters using the -nr option in the Linux command sort
Figure 4. Sorting in reverse order with mixed numbers and letters using the -nr option in the Linux command sort

-k: Sorting by a Specific Column

If your file has multiple columns, you can sort based on a specific column using the -k option.

sort -k 2 filename.txt
ShellScript

This command sorts the file based on the second column. For example, if your file contains:

banana 32
apple 17
tomato 2
cherry 108
raspberry 87
Plaintext

Running sort -k 2 filename.txt will sort the data based on the numeric values in the second column.

Figure 5. Sorting by the second column using the -k option in the Linux command sort
Figure 5. Sorting by the second column using the -k option in the Linux command sort

-t: Specifying a Delimiter

When data is separated by something other than whitespace, such as in a CSV file, you can use the -t option to specify a delimiter.

sort -t ',' -k 2 filename.csv
ShellScript

This command uses a comma as the delimiter and sorts the data based on the second column.

-u: Removing Duplicates

If you want to remove duplicate data while sorting, use the -u option. This option outputs only unique entries by consolidating duplicates.

sort -u 파일명.txt
ShellScript

For example, if the file contains:

apple
tomato
apple
raspberry
grape
apple
tomato
apple
tomato
apple
grape
Plaintext

After running the command, you’ll see that only the unique entries are kept, and the results are sorted in ascending order.

Figure 6. Removing duplicates using the -u option in the Linux command sort
Figure 6. Removing duplicates using the -u option in the Linux command sort

-o: Saving Sorted Results to a File

If you want to save the sorted results to a file, use the -o option.

sort filename.txt -o sorted_filename.txt
ShellScript

This command reads the content of filename.txt, sorts it, and saves the result to sorted_filename.txt.

Practical Examples of Using the sort Command

The sort command is not only useful for sorting files but also for tasks like data analysis or log file management. It becomes particularly powerful when dealing with large datasets, allowing you to sort based on specific fields, numbers, dates, times, and more.

For instance, if you want to extract and sort logs for a specific user from a system log file, you can combine the grep command with sort. This example uses grep to extract a user’s log entries and sorts them by the third column:

grep 'username' filename.txt | sort -k 3
ShellScript

The following figure shows an example where the third column of logs containing “WARNING” in /var/log/syslog is sorted.

Figure 7. Using the Linux command sort with grep to sort system logs
Figure 7. Using the Linux command sort with grep to sort system logs

Precautions

  • The sort command sorts alphabetically by default, so you should use the -n option when sorting numeric data.
  • When working with large datasets, sort may use significant system resources, so consider using memory-efficient options if necessary.
  • Be careful when using the -o option to avoid overwriting input files. If the input and output files are the same, the original file could become corrupted.

Summary

The Linux command sort is an essential tool for sorting data. From basic alphabetical sorting to more complex operations like sorting by specific columns or removing duplicates, sort offers a range of options to simplify complex data processing tasks. Whether you’re analyzing log files or managing large datasets, mastering this command can greatly enhance your data management capabilities. Familiarize yourself with its options to sort and manage data efficiently.

References

Leave a Comment