Skip to main content

How to Sort Lines in Bash

3 min read
bash sort data processing text manipulation sorting

Quick Answer: Sort Lines in Bash

To sort lines in a file, use the sort command: sort file.txt. For numeric sort, use sort -n. For reverse order, use sort -r. To sort by specific column in CSV, use sort -t',' -k2 for column 2.

Quick Comparison: Sorting Methods

MethodSyntaxBest ForPerformance
sortsort file.txtText/alphabeticalFast
sort -nsort -n numbers.txtNumbersFast
sort -rsort -r file.txtReverse orderFast
sort -ksort -k2 file.txtSpecific columnFast

Bottom line: Use sort for alphabetical; use sort -n for numeric sorting.


Sort lines efficiently in Bash. Whether you’re organizing data, preparing files for processing, or preparing output for display, understanding sort options and techniques is essential.

Method 1: Basic Alphabetical Sort

The default sort behavior is alphabetical (lexicographic).

# Sort alphabetically
sort file.txt

# Sort and save to new file
sort file.txt > sorted.txt

# Sort multiple files and combine
sort file1.txt file2.txt > combined_sorted.txt

# In-place sort (overwrite original)
sort -o file.txt file.txt

Example with sample file:

# Input (names.txt):
charlie
alice
bob

# Command: sort names.txt
# Output:
alice
bob
charlie

Method 2: Numeric Sort

Use -n flag to sort numbers correctly. Without it, “10” comes before “2”.

# Sort numerically
sort -n numbers.txt

# Reverse numeric sort
sort -rn numbers.txt

# Sort with decimal points
sort -n prices.txt

Example:

# Input:
10
2
30
1

# Default sort (wrong):
sort file.txt
# Output: 1, 10, 2, 30

# Numeric sort (correct):
sort -n file.txt
# Output: 1, 2, 10, 30

Method 3: Reverse Sort

The -r flag reverses the sort order.

# Sort in reverse (Z to A)
sort -r file.txt

# Reverse numeric sort
sort -rn file.txt

# Reverse and remove duplicates
sort -ru file.txt

Method 4: Sort by Specific Column

Use -k to sort by specific fields, useful for CSV and structured text.

# Sort by column 2
sort -k2 file.txt

# Sort by column 2, then column 3
sort -k2,2 -k3,3 file.txt

# For CSV files (comma-delimited), specify delimiter
sort -t',' -k2 data.csv

# Sort column 2 numerically
sort -t',' -k2 -n data.csv

# Sort multiple columns with different options
sort -t',' -k2,2nr -k3,3 data.csv

Example with CSV:

# Input (employees.csv):
name,department,salary
John,IT,50000
Jane,HR,45000
Bob,IT,60000

# Sort by department, then salary
sort -t',' -k2,2 -k3,3 employees.csv

# Output:
name,department,salary
Jane,HR,45000
Bob,IT,60000
John,IT,50000

Method 5: Case-Insensitive Sort

The -f flag ignores case differences.

# Case-insensitive sort
sort -f file.txt

# Case-insensitive with reverse
sort -fr file.txt

Example:

# Input:
Apple
banana
Carrot

# Default sort:
sort file.txt
# Output: Apple, Carrot, banana

# Case-insensitive:
sort -f file.txt
# Output: Apple, banana, Carrot

Method 6: Remove Duplicates While Sorting

Use -u to eliminate duplicate lines during sorting.

# Sort and remove duplicates
sort -u file.txt

# Numeric sort without duplicates
sort -nu file.txt

# Reverse sort without duplicates
sort -ru file.txt

Method 7: Human-Readable File Sizes

The -h flag sorts file sizes like “1K”, “2M”, “3G”.

# Sort file sizes (human-readable)
sort -h filesizes.txt

# Sort in reverse by size
sort -hr filesizes.txt

Example:

# Input:
512B
2.5K
1.2M
100M

# Without -h (wrong):
sort file.txt
# 100M, 1.2M, 2.5K, 512B

# With -h (correct):
sort -h file.txt
# 512B, 2.5K, 1.2M, 100M

Method 8: Month Sort

The -M flag sorts by month names.

# Sort by month names
sort -M months.txt

# Example:
# Input: Dec, Jan, Mar, Feb
# Output: Jan, Feb, Mar, Dec

Practical Examples

Example 1: Sort Log File by Timestamp

#!/bin/bash

log_file="$1"

# Assuming log format: YYYY-MM-DD HH:MM:SS ...
# Sort by date (column 1) and time (column 2)
sort -k1,1 -k2,2 "$log_file" > "${log_file%.log}_sorted.log"

echo "Sorted log file created"

Example 2: Sort CSV by Multiple Columns

#!/bin/bash

csv_file="$1"

# Sort by department (column 2), then salary (column 3) numerically descending
sort -t',' -k2,2 -k3,3rn "$csv_file" > "${csv_file%.csv}_sorted.csv"

echo "Sorted CSV created"

Input:

id,dept,salary,name
1,IT,50000,John
2,HR,45000,Jane
3,IT,60000,Bob
4,HR,55000,Alice

Output:

id,dept,salary,name
3,IT,60000,Bob
1,IT,50000,John
4,HR,55000,Alice
2,HR,45000,Jane

Example 3: Sort and Display Top N Items

#!/bin/bash

# Sort numbers and show top 5
sort -rn numbers.txt | head -5

# Sort file sizes and show largest 10
ls -lS /path | sort -k5 -rn | head -10

Example 4: Sort Network Connections by Activity

#!/bin/bash

# Sort network connections by byte count (common in netstat output)
# netstat -an | sort -k2 -rn

Example 5: Function for Flexible Sorting

#!/bin/bash

# Flexible sort function
sort_file() {
  local file="$1"
  local method="${2:-alphabetical}"
  local key="${3:-1}"
  local output="${file%.txt}_sorted.txt"

  case "$method" in
    alphabetical)
      sort "$file" > "$output"
      ;;
    numeric)
      sort -n "$file" > "$output"
      ;;
    reverse)
      sort -r "$file" > "$output"
      ;;
    column)
      sort -k"$key" "$file" > "$output"
      ;;
    *)
      echo "Unknown method: $method"
      return 1
      ;;
  esac

  echo "Sorted file: $output"
}

# Usage
sort_file "data.txt" "numeric"
sort_file "data.csv" "column" "2"

Example 6: Sort with Special Characters

#!/bin/bash

# Sort ignoring special characters and case
sort -f file.txt

# Sort with different collation rules
LC_ALL=C sort file.txt

Example 7: Sort Large File Efficiently

#!/bin/bash

# For very large files, use parallel sorting
file="$1"

# Estimate number of CPU cores
cores=$(grep -c "^processor" /proc/cpuinfo 2>/dev/null || echo 4)

# Sort with parallelization
sort --parallel="$cores" "$file" > "${file%.txt}_sorted.txt"

echo "Sorted using $cores cores"

Advanced Examples

Example 8: Sort with Complex Key

#!/bin/bash

# Sort by multiple keys with different options
# Example: Sort by year (descending), then month (ascending)

data="year,month,value
2023,12,100
2024,01,200
2023,01,150
2024,06,300"

echo "$data" | sort -t',' -k1,1rn -k2,2n

Output:

year,month,value
2024,01,200
2024,06,300
2023,12,100
2023,01,150

Example 9: Sort and Get Statistics

#!/bin/bash

# Sort numbers and calculate statistics
numbers_file="$1"

sorted=$(sort -n "$numbers_file")
count=$(echo "$sorted" | wc -l)
first=$(echo "$sorted" | head -1)
last=$(echo "$sorted" | tail -1)
middle_line=$(( (count + 1) / 2 ))
median=$(echo "$sorted" | sed -n "${middle_line}p")

echo "Count: $count"
echo "Min: $first"
echo "Max: $last"
echo "Median: $median"

Performance Comparison

For sorting large files:

MethodSpeedFlags
Basic alphabeticalVery Fast(none)
NumericVery Fast-n
Multi-columnFast-k/-t
Parallel (large files)Fastest—parallel

Best choice: Use sort -n for numbers, sort -t',' -k2,2 for CSV columns.

Important Considerations

Memory Usage

Large files might exceed available memory:

# Use -T to specify temp directory with more space
sort -T /tmp -n large_file.txt > output.txt

# Or write directly to file
sort input.txt -o output.txt

Locale and Collation

Sort behavior depends on locale settings:

# Use C locale for consistent ASCII sorting
LC_ALL=C sort file.txt

# Use current locale for natural sorting
LC_ALL=en_US.UTF-8 sort file.txt

Stability

Sort is stable: equal elements maintain relative order:

# With stable sort, secondary field order preserved
sort -k1,1 -k2,2rn file.txt

Key Points

  • Use sort for alphabetical sorting
  • Use sort -n for numeric values
  • Use sort -r for reverse order
  • Use sort -k for column-based sorting
  • Use sort -u to remove duplicates
  • Use sort -f for case-insensitive
  • Specify -t for non-space delimiters
  • Consider locale settings for proper collation

Quick Reference

# Basic sorts
sort file.txt                      # Alphabetical
sort -n file.txt                   # Numeric
sort -r file.txt                   # Reverse
sort -f file.txt                   # Case-insensitive

# Column sorting (CSV example)
sort -t',' -k2,2 file.csv          # By column 2
sort -t',' -k2,2n file.csv         # Column 2 numeric
sort -t',' -k2,2 -k3,3 file.csv    # Columns 2, then 3

# Utilities
sort -u file.txt                   # Remove duplicates
sort -h sizes.txt                  # Human-readable
sort -o file.txt file.txt          # In-place sort
#!/bin/bash

file="$1"

# For simple alphabetical sort:
sort "$file" > "${file}.sorted"

# For numeric sort:
sort -n "$file" > "${file}.sorted"

# For CSV with multiple sort keys:
sort -t',' -k2,2 -k3,3n "$file" > "${file}.sorted"