Cheatsheet & Examples: sort
The sort command is used to sort lines of text files in alphabetical, numerical, or other specified orders. It can also handle unique entries, reverse sorting, and field-based sorting.
Sorting a file alphabetically
Example Usage:sort filename.txt
What it does:
Sorts the contents of the file in ascending alphabetical order.
Command-line Arguments Explained:
filename.txt: The text file to be sorted.
Sorting numerically
Example Usage:sort -n filename.txt
What it does:
Sorts the file based on numeric values rather than lexicographical order.
Command-line Arguments Explained:
-n: Enables numeric sorting.
Sorting in reverse order
Example Usage:sort -r filename.txt
What it does:
Sorts the file in descending order (reverse of default behavior).
Command-line Arguments Explained:
-r: Reverses the sort order.
Removing duplicate lines
Example Usage:sort -u filename.txt
What it does:
Sorts the file and removes duplicate lines, keeping only unique entries.
Command-line Arguments Explained:
-u: Removes duplicate lines after sorting.
Sorting by specific fields
Example Usage:sort -k 2,2 filename.txt
What it does:
Sorts the file based on the second field (column), using whitespace as the default delimiter.
Command-line Arguments Explained:
-k 2,2: Specifies the sort key starting and ending at the second field.
Specifying a custom delimiter
Example Usage:sort -t ',' -k 2,2 filename.csv
What it does:
Sorts a CSV file by the second field, using a comma as the delimiter.
Command-line Arguments Explained:
-t ',': Sets the delimiter to a comma.-k 2,2: Sorts by the second field.
Merging multiple sorted files
Example Usage:sort -m file1.txt file2.txt
What it does:
Merges multiple already-sorted files into a single sorted output.
Command-line Arguments Explained:
-m: Merges sorted input files without re-sorting.
Checking if a file is already sorted
Example Usage:sort -c filename.txt
What it does:
Verifies if the file is already sorted; if not, it reports an error.
Command-line Arguments Explained:
-c: Checks the input for sorted order.
Performing a stable sort
Example Usage:sort -s filename.txt
What it does:
Sorts the file without changing the order of equal lines.
Command-line Arguments Explained:
-s: Ensures a stable sort (maintains original order for equal elements).
Ignoring case during sorting
Example Usage:sort -f filename.txt
What it does:
Sorts lines case-insensitively (e.g., treating "Apple" and "apple" as equal).
Command-line Arguments Explained:
-f: Ignores case distinctions during sorting.
Skipping leading whitespace or characters
Example Usage:sort -b filename.txt
What it does:
Ignores leading whitespace or characters before sorting.
Command-line Arguments Explained:
-b: Skips leading whitespace or characters in each line.
Redirecting output to a file
Example Usage:sort input.txt > output.txt
What it does:
Sorts the input file and writes the sorted output to a specified file.
Command-line Arguments Explained:
input.txt: The file to sort.> output.txt: Redirects the output tooutput.txtinstead of the terminal.

