sort

The sort command is a Linux/Unix command-line utility used to sort the lines of a text file or input stream in lexicographical order (alphabetical order for text and ascending order for numbers). It's a versatile tool with various options for customizing the sorting process.

SYNTAX: sort [options] [file]
Options: The sort command has several options that allow you to customize the sorting behavior. Some common options include:
    -r or --reverse: Reverse the order of the sort.
    -n or --numeric-sort: Treats the lines as numeric values.
    -k or --key: Specifies a key field for sorting.
    -b or --ignore-leading-blanks: Ignores leading whitespace.
    -f or --ignore-case: Performs a case-insensitive sort.
    -u or --unique: Removes duplicate lines.
    -t or --field-separator: Specifies a field separator.
                

Examples

Sort lines in a file
sort filename
Sort lines numerically
sort -n filename
Reverse sort lines
sort -r filename
Sort and remove duplicate lines
sort -u filename
Sort a specific field in a csv file
sort -t',' -k2,2 filename.csv
Sort lines in reverse numerical order
sort -n -r filename
Sort lines based on a column using whitespace as delimiter
sort -k2,2 -t' ' filename.txt
Sort and merge pre-sorted files
sort -m file1 file2
Sort files based on modification time
ls -lt | sort -k6,7
Sort lines in random order
sort -R filename
Sort and print unique values from a column
sort -u -k3,3 filename.txt
Sort and display human-readable file sizes
du -h | sort -h
Sort ip addresses in numerical order
sort -t. -k1,1n -k2,2n -k3,3n -k4,4n filename.txt
Sort lines based on the last field in a csv file
sort -t',' -kNF,NF filename.csv
Sort files based on access time
ls -lt --time=atime | sort -k6,7
Sort and print only unique lines
sort -u filename
Sort lines based on field 2 and 3 numerically
sort -n -k2,2 -k3,3 filename.txt
Sort lines in reverse order based on the second column
sort -k2,2r filename.txt
Sort and remove leading whitespaces in a file
cat filename | sort | sed 's/^ *//'
Sort lines based on field 3, case-insensitive
sort -f -k3,3 filename.txt
Sort lines in descending order based on numeric values
sort -n -r filename
Sort lines based on field 2, ignore leading whitespaces
sort -k2,2n -b filename.txt
Sort lines based on field 2, skip leading whitespace
sort -k2,2 -b filename.txt
Sort lines based on the last character in each line
rev filename | sort | rev
Sort and count occurrences of each line
sort filename | uniq -c
Sort by month (assuming month names in a column)
sort -M -k2 filename.txt
Sort by file size in human-readable format
ls -lh | sort -h -k5
Sort lines based on the second and third fields numerically
sort -n -k2,2 -k3,3 filename.txt
Sort lines based on field 2, case-insensitive
sort -f -k2,2 filename.txt
Sort and display unique lines, counting repeated lines
sort filename | uniq -c
Sort lines in reverse order based on field 3
sort -k3,3r filename.txt
Sort lines based on field 2 in a csv file, ignore case
sort -t',' -f -k2,2 filename.csv
Sort lines based on field 1 and 2 in numerical order
sort -n -k1,1 -k2,2 filename.txt
Sort lines based on field 2, ignore case and leading whitespaces
sort -k2,2f -b filename.txt
Sort and display unique lines, case-insensitive
sort -fu filename
Sort and display unique lines, ignore leading whitespaces
sort -bu filename
Sort lines based on field 2 and 3, numeric and case-insensitive
sort -n -f -k2,2 -k3,3 filename.txt
Sort and display unique lines, counting repeated lines, ignore case
sort -fu filename | uniq -ci
Sort lines based on field 3, numeric and case-insensitive
sort -n -f -k3,3 filename.txt
Sort lines based on field 2, reverse numerical order
sort -n -r -k2,2 filename.txt