GNU sort Cheatsheet
#
GNU sort
is a command-line utility for sorting lines of text files.
Basic Usage
#
Command/Option | Example | Description |
---|
sort file.txt | sort data.txt | Sort lines of a file |
sort -o file.txt file.txt | sort -o sorted.txt unsorted.txt | Sort lines and save to a file |
sort -r file.txt | sort -r data.txt | Sort lines in reverse order |
Numeric and General Sorting
#
Command/Option | Example | Description |
---|
sort -n file.txt | sort -n data.txt | Sort lines numerically |
sort -g file.txt | sort -g data.txt | Sort lines using general numeric sorting |
Key Sorting
#
Command/Option | Example | Description |
---|
sort -k2 file.txt | sort -k2 data.txt | Sort lines based on the second field |
sort -k2,3 file.txt | sort -k2,3 data.txt | Sort lines based on fields 2 to 3 |
Field and Character Specific Sorting
#
Command/Option | Example | Description |
---|
sort -b file.txt | sort -b data.txt | Ignore leading blanks |
sort -t, file.txt | sort -t, data.txt | Use comma as the field separator |
sort -s file.txt | sort -s data.txt | Stable sort |
Ignore Case
#
Command/Option | Example | Description |
---|
sort -f file.txt | sort -f data.txt | Ignore case when sorting |
Unique Sorting
#
Command/Option | Example | Description |
---|
sort -u file.txt | sort -u data.txt | Sort lines and remove duplicates |
This cheatsheet provides essential GNU sort
commands for sorting lines in text files.