The sort
command in Linux is used to sort lines of text files. It can arrange the text in various orders, such as alphabetical, numerical, or based on specific fields. This is useful for organizing and managing data within text files.
Initial Example
Using sort
to sort the lines of a file called example.txt
in alphabetical order:
sort example.txt
SORT Parameters
Parameter | Description |
---|---|
-b, --ignore-leading-blanks | Ignore leading blank spaces in sorting. |
-d, --dictionary-order | Only consider blanks and alphanumeric characters for sorting. |
-f, --ignore-case | Treat lowercase and uppercase characters as equivalent. |
-g, --general-numeric-sort | Sort lines based on general numerical value. |
-i, --ignore-nonprinting | Ignore non-printable characters in sorting. |
-M, --month-sort | Sort by month, with unknowns < ‘JAN’ < … < ‘DEC’. |
-h, --human-numeric-sort | Sort human-readable numbers (e.g., 2K, 1G). |
-n, --numeric-sort | Sort lines based on numerical value. |
-R, --random-sort | Shuffle lines randomly, keeping identical keys grouped. |
--random-source=FILE | Use FILE for random byte generation. |
-r, --reverse | Reverse the sort order. |
--sort=WORD | Sort based on WORD: general-numeric , human-numeric , month , numeric , random , version . |
-V, --version-sort | Sort version numbers within text naturally. |
--batch-size=NMERGE | Merge up to NMERGE inputs simultaneously; use temporary files for more. |
-c, --check, --check=diagnose-first | Check if input is sorted, without sorting. |
-C, --check=quiet, --check=silent | Like -c , but suppress the output of the first unsorted line. |
--compress-program=PROG | Compress and decompress temporary files using PROG. |
--debug | Annotate sorted line parts and warn about potential issues. |
--files0-from=F | Read input from files specified by NUL-terminated names in F; use - for standard input. |
-k, --key=KEYDEF | Sort based on a specific key; KEYDEF defines the key’s location and type. |
-m, --merge | Merge already sorted input files without sorting them. |
-o, --output=FILE | Write output to FILE instead of standard output. |
-s, --stable | Ensure stable sorting by disabling the last-resort comparison. |
-S, --buffer-size=SIZE | Use SIZE for the main memory buffer. |
-t, --field-separator=SEP | Use SEP as the field separator instead of whitespace. |
-T, --temporary-directory=DIR | Use DIR for temporary files; specify multiple directories with multiple options. |
--parallel=N | Run N sorts concurrently. |
-u, --unique | With -c , check for strict order; without -c , output only the first of equal lines. |
-z, --zero-terminated | Use NUL as the line delimiter instead of newline. |
--help | Display help information and exit. |
--version | Show version information and exit. |
Examples
1. Sort Lines Alphabetically
To sort the lines of a file in alphabetical order:
sort filename.txt
2. Sort Lines Numerically
To sort the lines of a file based on numerical values:
sort -n filename.txt
3. Sort Lines in Reverse Order
To sort the lines of a file in reverse order:
sort -r filename.txt
4. Sort Based on a Specific Field
To sort the lines of a file based on the values in a specific field (e.g., the second field):
sort -k 2 filename.txt
5. Sort with a Custom Field Separator
To specify a custom field separator (e.g., a comma):
sort -t, -k 2 filename.txt
6. Remove Duplicate Lines
To sort and remove duplicate lines from a file:
sort -u filename.txt
7. Sort Multiple Files
To sort the contents of multiple files and combine the output:
sort file1.txt file2.txt -o sorted_output.txt