Linux sort Command


The sort command in Linux arranges lines in a file or input based on various criteria, such as alphabetical order, numerical order, and more. It’s commonly used for data processing, where sorting a file or output in a specific order is required.

Basic Syntax

sort [options] filename

Common Options

  • -r : Sorts in reverse order.
  • -n : Sorts by numeric value.
  • -k : Specifies a column (or field) to sort by.
  • -u : Removes duplicate lines.
  • -t : Specifies a delimiter for sorting by fields (default is whitespace).

Examples with Output

Example 1: Sorting a File Alphabetically

Suppose file.txt contains:

banana apple cherry date
Command
sort file.txt
Output
apple banana cherry date

Explanation:

  • sort file.txt arranges the lines alphabetically in ascending order.

Example 2: Sorting in Reverse Order

You can reverse the order of sorting using -r.

Command
sort -r file.txt
Output
date cherry banana apple

Explanation:

  • sort -r file.txt sorts the lines in reverse alphabetical order.

Example 3: Sorting Numerically

For files with numbers, use -n to sort based on numeric values.

Suppose numbers.txt contains:

10 1 20 2
Command
sort -n numbers.txt
Output
1 2 10 20

Explanation:

  • sort -n treats each line as a number, sorting numerically.

Example 4: Sorting by a Specific Column

Suppose data.txt contains two columns of data separated by spaces:

apple 3 banana 1 cherry 2 date 5
Command
sort -k 2 data.txt
Output
banana 1 cherry 2 apple 3 date 5

Explanation:

  • -k 2 sorts based on the second column, which is the numeric values in this case.

Example 5: Sorting by a Delimited Field

If fields are separated by a specific delimiter, use -t with -k.

Suppose data.csv contains:

apple,3 banana,1 cherry,2 date,5
Command
sort -t ',' -k 2 data.csv
Output
banana,1 cherry,2 apple,3 date,5

Explanation:

  • -t ',' specifies a comma as the delimiter.
  • -k 2 sorts by the second field, which is the numeric value after the comma.

Example 6: Removing Duplicate Lines

If a file has duplicate lines, -u can remove them while sorting.

Suppose duplicates.txt contains:

apple banana apple cherry banana
Command
sort -u duplicates.txt
Output
apple banana cherry

Explanation:

  • sort -u removes duplicate lines, leaving only unique entries in sorted order.

Summary of Common sort Commands

CommandDescription
sort file.txtSorts lines alphabetically in ascending order.
sort -r file.txtSorts lines in reverse alphabetical order.
sort -n numbers.txtSorts lines numerically.
sort -k 2 data.txtSorts by the second column (field) in whitespace-separated data.
sort -t ',' -k 2 data.csvSorts by the second field in comma-separated data.
sort -u duplicates.txtSorts and removes duplicate lines.

The sort command is an essential tool for organizing and managing data, particularly in scripts and data processing workflows.