sort / uniq / comm Cheatsheet
sort, uniq, and comm are three fundamental Linux/Unix text processing commands. sort orders lines, uniq deduplicates and counts frequencies, comm performs set operations (intersection, difference). This cheatsheet covers everything from basics to advanced usage.
Basic Sort
The sort command orders text lines, defaulting to lexicographic order.
### Basic Operations
``yaml
# Sort lexicographically
sort file.txt
# Sort numerically sort -n numbers.txt
# Reverse order sort -r file.txt
# Numeric reverse sort -rn numbers.txt
# Case-insensitive sort sort -f file.txt
# Ignore leading blanks
sort -b file.txt
`
### Check Sort Status
`yaml
# Check if already sorted (no output = sorted)
sort -c file.txt
# Silent check (exit code indicates status, 0=sorted)
sort -C file.txt
`
Advanced Sort
### Field-Based Sorting
`yaml
# Sort by the 2nd column (tab/space delimited)
sort -k2 data.txt
# Sort by the 3rd column numerically sort -k3n data.txt
# Sort by 2nd, then 3rd numerically sort -k2,2 -k3n data.txt
# Specify column delimiter (colon) sort -t: -k3n /etc/passwd
# Keep header row when sorting
(head -1 data.txt && tail -n+2 data.txt | sort -k2n) > sorted.txt
`
### Human-Readable and Version Sorting
`yaml
# Human-readable numeric sort (K/M/G suffixes)
sort -h sizes.txt
# Version number sort sort -V versions.txt
# Mixed sort
sort -h -k2 size_report.txt
`
### Merge and Output Control
`yaml
# Merge already sorted files
sort -m sorted1.txt sorted2.txt
# Sort and deduplicate sort -u file.txt
# Write to file instead of stdout sort file.txt -o sorted.txt
# Specify buffer size sort -S 1G large.txt
# Specify temporary directory sort -T /tmp large.txt
# Random sorting
sort --random-sort file.txt
`
Deduplication (uniq)
The uniq command removes or counts consecutive duplicate lines (usually used after sort).
### Basic Dedup
`yaml
# Remove consecutive duplicates (usually sort first)
sort file.txt | uniq
# Inline dedup (equivalent to sort -u) sort -u file.txt
# Count occurrences of each line sort file.txt | uniq -c
# Show only duplicate lines sort file.txt | uniq -d
# Show only unique lines (non-repeating)
sort file.txt | uniq -u
`
### Advanced Dedup
`yaml
# Case-insensitive dedup
sort file.txt | uniq -i
# Compare only first N characters sort file.txt | uniq -w 5
# Skip first N characters sort data.txt | uniq -s 3
# Count with case-insensitivity sort -f file.txt | uniq -ic
# Count and sort by frequency (descending)
sort file.txt | uniq -c | sort -rn
`
Set Operations (comm)
The comm command compares two sorted files line by line.
### Intersection and Difference
`yaml
# Show all three columns (file1-only | file2-only | common)
comm sorted1.txt sorted2.txt
# Intersection: lines common to both comm -12 file1.txt file2.txt
# file1-only lines (difference A-B) comm -23 file1.txt file2.txt
# file2-only lines (difference B-A)
comm -13 file1.txt file2.txt
`
### Set Operation Combinations
`yaml
# Symmetric difference (A∪B - A∩B)
comm -3 file1.txt file2.txt | tr -d '\t'
# Union (all unique lines) cat file1.txt file2.txt | sort -u
# Check if A is a subset of B
# If A-B is empty then A⊆B
[ -z "$(comm -23 A.txt B.txt)" ] && echo "A is a subset of B"
`
Pipeline Patterns
sort, uniq, and comm are frequently combined with other commands in powerful text processing pipelines.
### Log Analysis
`yaml
# Count Nginx access IPs (descending)
awk '{print $1}' access.log | sort | uniq -c | sort -rn
# Count HTTP status code distribution awk '{print $9}' access.log | sort | uniq -c | sort -rn
# Find top 10 most active IPs
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -10
`
### File Dedup and Sorting
`yaml
# Merge multiple files and deduplicate
cat file1.txt file2.txt file3.txt | sort -u > combined.txt
# Find common and different lines between two files
sort file1.txt > a.sorted
sort file2.txt > b.sorted
comm a.sorted b.sorted
`
### Utility Commands
`yaml
# Shuffle lines randomly
shuf file.txt
# Topological sort (dependency ordering) tsort depend.txt
# Count word frequency tr ' ' '\n' < file.txt | sort | uniq -c | sort -rn
# Count unique lines in a file cat file.txt | sort | uniq | wc -l ``
Basic Sort(8)
| Command | Level | ||
|---|---|---|---|
sort file.txtSort lexicographically in ascending order | Basic | sort file.txt | |
sort -n numbers.txtSort numerically | Basic | sort -n numbers.txt | |
sort -r file.txtReverse (descending) sort | Basic | sort -r file.txt | |
sort -rn numbers.txtNumeric reverse sort | Basic | sort -rn numbers.txt | |
sort -f file.txtCase-insensitive sort | Basic | sort -f file.txt | |
sort -b file.txtIgnore leading blanks | Basic | sort -b file.txt | |
sort -c file.txtCheck if file is sorted (output first wrong line) | Intermediate | sort -c file.txt | |
sort -C file.txtSilent sort check (exit code 0=already sorted) | Intermediate | sort -C file.txt |
Advanced Sort(12)
| Command | Level | ||
|---|---|---|---|
sort -k2 data.txtSort by the 2nd field | Intermediate | sort -k2 data.txt | |
sort -k3n data.txtSort by the 3rd field numerically | Intermediate | sort -k3n data.txt | |
sort -t: -k3n /etc/passwdSort by 3rd field numerically with colon delimiter | Intermediate | sort -t: -k3n /etc/passwd | |
sort -k2,2 -k3n data.txtSort by 2nd field, then by 3rd field numerically | Intermediate | sort -k2,2 -k3n data.txt | |
sort -h sizes.txtHuman-readable numeric sort (K/M/G suffixes) | Intermediate | sort -h sizes.txt | |
sort -V versions.txtVersion number sort | Intermediate | sort -V versions.txt | |
sort -u file.txtSort and remove duplicates | Basic | sort -u file.txt | |
sort -m sorted1.txt sorted2.txtMerge already sorted files (no sorting) | Expert | sort -m sorted1.txt sorted2.txt | |
sort -S 1G large.txtSpecify sort buffer size | Expert | sort -S 1G large.txt | |
sort -T /tmp large.txtSpecify temporary directory | Expert | sort -T /tmp large.txt | |
sort -o sorted.txt file.txtSort and write output to a file | Basic | sort file.txt -o sorted.txt | |
sort --random-sort file.txtRandom sort (shuffle lines) | Intermediate | sort --random-sort file.txt |
Dedup(6)
| Command | Level | ||
|---|---|---|---|
sort file.txt | uniqSort then remove duplicates | Basic | sort file.txt | uniq | |
sort file.txt | uniq -cCount occurrences of each line | Basic | sort file.txt | uniq -c | |
sort file.txt | uniq -dShow only duplicate lines | Basic | sort file.txt | uniq -d | |
sort file.txt | uniq -uShow only unique lines (non-repeating) | Basic | sort file.txt | uniq -u | |
sort -f file.txt | uniq -icCase-insensitive count | Intermediate | sort -f file.txt | uniq -ic | |
sort file.txt | uniq -w 5Dedup comparing only first 5 characters | Intermediate | sort file.txt | uniq -w 5 |
Set Ops(4)
| Command | Level | ||
|---|---|---|---|
comm sorted1.txt sorted2.txtShow three columns: file1-only|file2-only|common | Basic | comm sorted1.txt sorted2.txt | |
comm -12 file1.txt file2.txtIntersection (lines common to both files) | Basic | comm -12 file1.txt file2.txt | |
comm -23 file1.txt file2.txtDifference (file1-only lines) | Basic | comm -23 file1.txt file2.txt | |
comm -13 file1.txt file2.txtDifference (file2-only lines) | Basic | comm -13 file1.txt file2.txt |
Pipeline(6)
| Command | Level | ||
|---|---|---|---|
sort file.txt | uniq -c | sort -rnCount frequencies and sort descending | Intermediate | sort file.txt | uniq -c | sort -rn | |
awk '{print $1}' access.log | sort | uniq -c | sort -rnCount IP access frequencies from logs, descending | Intermediate | awk '{print $1}' access.log | sort | uniq -c | sort -rn | |
cat file1.txt file2.txt | sort -uMerge files and deduplicate (union) | Basic | cat file1.txt file2.txt | sort -u | |
shuf file.txtRandomly shuffle file lines | Basic | shuf file.txt | |
tsort depend.txtTopological sort for dependency ordering | Expert | tsort depend.txt | |
tr ' ' '\n' < file.txt | sort | uniq -c | sort -rnCount word frequency (split spaces to lines) | Intermediate | tr ' ' '\n' < file.txt | sort | uniq -c | sort -rn |