SORT - Sort lines
sort file.txt
sort -r file.txt
sort -n numbers.txt
sort -f file.txt
Sort by column
sort -k 2 file.txt
sort -k 2 -n file.txt
sort -k 1,1 -k 2,2n file.txt
sort -t ',' -k 2 file.csv
Sort options
sort -h sizes.txt
sort -V versions.txt
sort -M months.txt
sort -R file.txt
sort -u file.txt
UNIQ - Remove duplicates
uniq file.txt
uniq -c file.txt
uniq -d file.txt
uniq -u file.txt
uniq -i file.txt
Sort + Uniq combination
sort file.txt | uniq
sort -u file.txt
sort file.txt | uniq -c
sort file.txt | uniq -c | sort -rn
Advanced sorting
ls -l | sort -k 5 -n
ls -lt
sort -t ':' -k 3 -n /etc/passwd
sort -b file.txt
sort -s file.txt
Field-specific sorting
sort -k 9 access.log
sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n ips.txt
sort -t ',' -k 3 data.csv
Ignore fields
uniq -f 1 file.txt
uniq -s 10 file.txt
Practical examples
Count unique visitors (access log)
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -10
Find duplicates
sort file.txt | uniq -d
Top 10 most common items
sort file.txt | uniq -c | sort -rn | head -10
Merge sorted files
sort -m file1.txt file2.txt
Remove duplicates from file
sort file.txt | uniq > file-unique.txt
sort -u file.txt -o file.txt
Sort in-place
sort file.txt -o file.txt
sort file.txt > temp && mv temp file.txt
Check if sorted
sort -c file.txt
echo $?
sort -c file.txt 2>&1
Numeric vs alphabetic
sort numbers.txt
sort -n numbers.txt
Complex sorting
sort -k 2,2n -k 1,1 file.txt
sort -k 1 file.txt
sort -t ',' -k 3,3 -k 1,1 data.csv
Case studies
Website access analysis
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -10
awk '{print $7}' access.log | sort | uniq -c | sort -rn | head -10
awk '{print $9}' access.log | sort | uniq -c | sort -n
Log analysis
grep ERROR app.log | sort | uniq -c | sort -rn
grep ERROR app.log | cut -d: -f3- | sort | uniq
Data deduplication
sort -u ips.txt -o ips-unique.txt
awk '!seen[$0]++' file.txt
Find missing numbers
seq 1 100 > expected.txt
sort -n actual.txt > actual-sorted.txt
comm -23 expected.txt actual-sorted.txt
Performance tips
sort --parallel=4 large-file.txt
sort -S 2G large-file.txt
sort -T /tmp large-file.txt
sort --compress-program=gzip large-file.txt
Locale affecting sort
LC_ALL=C sort file.txt
LC_ALL=en_US.UTF-8 sort file.txt
export LC_ALL=C
sort file.txt
Alternative tools
awk '!seen[$0]++' file.txt
perl -ne 'print unless $seen{$_}++' file.txt
sort file.txt | python -c "import sys; print(''.join(sorted(set(sys.stdin))))"
Quick reference
sort file.txt
sort -n file.txt
sort -r file.txt
sort -u file.txt
sort -k 2 file.txt
sort -t ',' -k 2 file.csv
uniq file.txt
uniq -c file.txt
uniq -d file.txt
uniq -u file.txt
sort file.txt | uniq -c | sort -rn
sort -u file.txt