sort
sort
is a command-line utility used to sort lines of text files. It can sort data in various ways, such as numerically, alphabetically, or by specific fields.
Basic Syntax
Commonly Used Options
-b
: Ignore leading blanks.-d
: Consider only blanks and alphanumeric characters.-f
: Fold lowercase to uppercase characters.-g
: Compare according to general numerical value.-i
: Consider only printable characters.-M
: Compare (unknown) < 'JAN' < ... < 'DEC'.-n
: Compare according to string numerical value.-r
: Reverse the result of comparisons.-k pos1[,pos2]
: Sort by a key.-t char
: Usechar
as the field separator.-u
: Output only the first of an equal run.-o file
: Write result tofile
instead of standard output.-c
: Check for sorted input; do not sort.-C
: Check for sorted input; do not sort, and exit with status 0 if the input is sorted.
Examples
Basic Sort
Sort the lines in a file alphabetically:
Sort and Save to a File
Sort the lines in a file and save the result to another file:
Numeric Sort
Sort the lines in a file numerically:
Reverse Sort
Sort the lines in a file in reverse order:
Sort by a Specific Field
Sort the lines in a file by the second field (assuming fields are separated by spaces):
Sort by a Specific Field with a Custom Separator
Sort the lines in a file by the second field, using a comma as the field separator:
Ignore Leading Blanks
Sort the lines in a file, ignoring leading blanks:
Case-Insensitive Sort
Sort the lines in a file, ignoring case differences:
Unique Sort
Sort the lines in a file and remove duplicates:
Check if File is Sorted
Check if the lines in a file are sorted:
Sort by Month
Sort the lines in a file by month name:
Conclusion
sort
is a versatile tool for sorting lines of text files based on various criteria. Understanding its options allows for efficient and powerful text sorting directly from the command line.