sort

GNU sort Cheatsheet #

GNU sort is a command-line utility for sorting lines of text files.

Basic Usage #

Command/OptionExampleDescription
sort file.txtsort data.txtSort lines of a file
sort -o file.txt file.txtsort -o sorted.txt unsorted.txtSort lines and save to a file
sort -r file.txtsort -r data.txtSort lines in reverse order

Numeric and General Sorting #

Command/OptionExampleDescription
sort -n file.txtsort -n data.txtSort lines numerically
sort -g file.txtsort -g data.txtSort lines using general numeric sorting

Key Sorting #

Command/OptionExampleDescription
sort -k2 file.txtsort -k2 data.txtSort lines based on the second field
sort -k2,3 file.txtsort -k2,3 data.txtSort lines based on fields 2 to 3

Field and Character Specific Sorting #

Command/OptionExampleDescription
sort -b file.txtsort -b data.txtIgnore leading blanks
sort -t, file.txtsort -t, data.txtUse comma as the field separator
sort -s file.txtsort -s data.txtStable sort

Ignore Case #

Command/OptionExampleDescription
sort -f file.txtsort -f data.txtIgnore case when sorting

Unique Sorting #

Command/OptionExampleDescription
sort -u file.txtsort -u data.txtSort lines and remove duplicates

This cheatsheet provides essential GNU sort commands for sorting lines in text files.

sort

Explore our comprehensive cheatsheets to enhance your knowledge and efficiency. Each cheatsheet provides detailed command options, examples, and descriptions to help you master various tools and technologies.