sort

sort

The sort command in Unix and Linux is used to sort lines of text files. It can sort the lines alphabetically, numerically, or based on various other criteria. The sort command can also handle complex sorting tasks with various options to customize its behavior.

Basic Usage

The basic syntax for the sort command is:

sort [options] [file...]
  • file: The file(s) to be sorted. If no file is specified, sort reads from standard input.

Examples

Sorting a File Alphabetically

To sort the lines in a file alphabetically:

sort file.txt

Sorting a File and Saving the Output

To sort a file and save the sorted output to another file:

sort file.txt > sorted_file.txt

Options

Sorting in Reverse Order

To sort in reverse order, use the -r option:

Sorting Numerically

To sort numerically, use the -n option. This is useful when dealing with numbers:

Sorting by a Specific Field

To sort by a specific field (column), use the -k option followed by the field number. Fields are typically separated by spaces or tabs:

This command sorts file.txt by the second field.

Sorting by Multiple Fields

You can sort by multiple fields by specifying multiple -k options:

This command sorts by the second field, and if there are ties, it sorts by the third field.

Ignoring Case

To perform a case-insensitive sort, use the -f option:

Removing Duplicates

To remove duplicate lines, use the -u option:

Advanced Options

Sorting with a Custom Delimiter

To sort based on a custom delimiter, use the -t option followed by the delimiter character. For example, to sort by a comma-separated value (CSV) file:

Sorting by Human-Readable Numbers

If your file contains human-readable numbers (e.g., 1K, 2M), use the -h option to sort these correctly:

Sorting with Stable Sort

To ensure that the original order is preserved for equal elements, use the -s option:

Sorting by Month

If your file contains dates with month names and you want to sort them correctly, use the -M option:

Practical Use Cases

Sorting a List of Names

To sort a list of names alphabetically:

Sorting Log Files by Date

Assuming your log file entries start with a date, you can sort them chronologically:

Sorting Disk Usage

To sort the output of the du command (disk usage) by size:

Combining with Other Commands

Sorting and Removing Duplicates

To sort a file and remove duplicate lines, you can combine sort with uniq:

Or use the -u option directly with sort:

Sorting Pipelined Input

You can pipe the output of other commands into sort:

This command lists files in the current directory and sorts them by size (fifth column) numerically.

Summary

The sort command is a powerful and versatile tool for sorting lines of text in Unix and Linux. By understanding and using its various options, you can sort files alphabetically, numerically, and in many other ways. This command is essential for data processing, log analysis, and numerous other tasks.

help

Last updated