Sorting
sort <file>
Sort lines alphabetically
sort -r <file>
Reverse the order
sort -n <file>
Numeric sort
Without -n, 10 sorts before 9.
sort -h <file>
Human sizes such as 2K, 1G
Pairs well with du -h output.
sort -u <file>
Keep one of each equal line
sort -V <file>
Version sort (1.2 before 1.10)
sort -f <file>
Ignore case
sort -M <file>
Sort by month name
sort -g <file>
General numeric (floats, 1e3)
sort -R <file>
Shuffle, equal lines stay together
sort -d <file>
Dictionary order: blanks and alphanumerics
sort -b <file>
Ignore leading blanks
sort -s <file>
Stable: keep input order on ties
sort -o <file> <file>
Write result in place
Plain > redirect would empty the input first.
sort -c <file>
Check if already sorted
sort -m <a> <b>
Merge already sorted files
sort -z <file>
NUL-delimited lines
sort -S <size> -T <dir> <file>
Set buffer size and temp dir
sort --parallel=<n> <file>
Use n threads
Sorting by key
sort -k <n> <file>
Sort from field n to end of line
Use -k n,n to sort on that field alone.
sort -k <n>,<n> <file>
Sort on field n only
sort -t <sep> -k <n> <file>
Set the field separator
Without -t, fields split where blanks meet text.
sort -k <n>n,<n> <file>
Numeric on one field
sort -k <a>,<a> -k <b>nr,<b>
<file>
Primary then secondary key
sort -k <n>.<c> <file>
Start at character c of field n
sort -t$'\t' -k <n> <file>
Tab-separated (bash quoting)
sort --debug -k <n> <file>
Show which part is compared
uniq
uniq <file>
Collapse adjacent duplicates
uniq -c <file>
Prefix each line with its count
uniq -d <file>
Only repeated lines, one each
uniq -u <file>
Only lines that never repeat
uniq -D <file>
Every repeated line, all copies
uniq -i <file>
Ignore case
uniq -f <n> <file>
Skip first n fields when comparing
uniq -s <n> <file>
Skip first n characters
uniq -w <n> <file>
Compare only first n characters
uniq --group <file>
Blank line between groups
uniq <in> <out>
Write to a file argument
sort <file> | uniq -c | sort -rn
Rank lines by frequency
sort <a> <b> | uniq -d
Lines present in both sorted lists
Only exact if each list has no internal duplicates; otherwise
use comm.
cut
cut -f <list> <file>
Select tab-separated fields
cut -d <sep> -f <list> <file>
Fields with a custom delimiter
The delimiter must be a single character.
cut -c <list> <file>
Select by character position
cut -b <list> <file>
Select by byte position
cut -f 2- <file>
From field 2 to the end
Ranges: N, N-M, N-, -M, comma separated.
cut -f <list> --complement <file>
All fields except these
cut -s -f <list> <file>
Skip lines with no delimiter
Otherwise such lines are printed whole.
cut --output-delimiter=<str> -f <list>
Change the output separator
cut -z -f <list>
NUL-delimited lines