Home > Text Processing & Search > sort

sort: Sorting Text Files

The `sort` command is a powerful utility used to sort the contents of text files line by line. It can sort based on various criteria such as alphabetical order, numerical order, and month order. Notably, the `-n` option allows it to recognize numbers as actual numerical values rather than strings, ensuring correct sorting, which is extremely useful for log files or data analysis.

Overview

The `sort` command rearranges each line of a text file according to specified criteria and outputs it to standard output. By default, it sorts alphabetically, but various options allow sorting by numbers, months, or specific fields. The `-n` option is essential for correctly sorting numbers by treating them as numerical values rather than strings.

Key Features

  • Sorts lines of text files
  • Supports various sorting criteria like alphabetical, numerical, and monthly
  • Provides functionality for removing duplicates and sorting by specific fields
  • Can process both standard input and file input

Key Options

The `sort` command supports a wide range of options to provide powerful sorting capabilities. Here are some of the most commonly used options.

Sorting Criteria

Output and Others

Generated command:

Try combining the commands.

Description:

`sort` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Here are practical examples of using the `sort` command with its key options.

Basic Numerical Sort

cat numbers.txt
# Example Output:
# 10
# 2
# 1
# 20

sort -n numbers.txt
# Expected Output:
# 1
# 2
# 10
# 20

Sorts the contents of numbers.txt numerically in ascending order.

Reverse Numerical Sort

sort -nr numbers.txt
# Expected Output:
# 20
# 10
# 2
# 1

Sorts the contents of numbers.txt numerically in descending order.

Sort by Specific Field (Numerical)

cat data.csv
# Example Output:
# apple,100,red
# banana,20,yellow
# cherry,50,red

sort -t, -nk2 data.csv
# Expected Output:
# banana,20,yellow
# cherry,50,red
# apple,100,red

Sorts data.csv numerically based on the second field, using a comma (,) as the delimiter.

Numerical Sort with Duplicate Removal

cat numbers_dup.txt
# Example Output:
# 10
# 2
# 1
# 20
# 2

sort -nu numbers_dup.txt
# Expected Output:
# 1
# 2
# 10
# 20

Sorts numbers_dup.txt numerically and removes duplicate lines.

Save Sort Results to File

sort -n input.txt -o sorted_numbers.txt
cat sorted_numbers.txt

Sorts input.txt numerically and saves the results to sorted_numbers.txt.

Tips & Precautions

The `sort` command is very powerful, but incorrect usage can lead to unexpected results. Refer to the following tips and precautions for efficient utilization.

Useful Tips

  • `sort` performs a stable sort by default, meaning the relative order of records with equal keys is preserved.
  • When using the `-k` option, field numbers start from 1.
  • For sorting large files, specifying a temporary directory with the `-T` option can improve performance.
  • It is often used with pipes (`|`) to sort the output of other commands (e.g., `ls -l | sort -nk5`).

Precautions

  • Without the `-n` option, sorting numbers alphabetically can lead to results like '10' appearing before '2'. Always use `-n` for numerical sorting.
  • The sorting order can vary depending on locale settings. To ensure consistent results, you can explicitly set the locale, for example, using `LC_ALL=C sort`.

Same category commands