sort
sort is a standard command line program of Unix and Unix-like operating systems, that prints the lines of its input or concatenation of all files listed in its argument list in sorted order. Sorting is done based on one or more sort keys extracted from each line of input. [1]
- GNU Coreutils [EN] @ Fedora Package
- GNU Coreutils [EN] @ Homebrew Formula
Documentation
- sort [EN] @ GNU Coreutils Manual
- man 1 'sort' [EN]
Parameters
- -f, --ignore-case
- Fold lowercase characters into the equivalent uppercase characters when comparing so that, for example, b and B sort as equal. The LC_CTYPE locale determines character types.
- -h, --human-numeric-sort
- Sort numerically, first by numeric sign (negative, zero, or positive); then by SI suffix (either empty, or k or K, or one of MGTPEZYRQ, in that order); and finally by numeric value.
- -k POS1[,POS2], --key=POS1[,POS2]
- Specify a sort field that consists of the part of the line between POS1 and POS2 (or the end of the line, if POS2 is omitted), inclusive.
- -n, --numeric-sort
- Sort numerically. The number begins each line and consists of optional blanks, an optional - sign, and zero or more digits possibly separated by thousands separators, optionally followed by a decimal-point character and zero or more digits. An empty number is treated as 0. Signs on zeros and leading zeros do not affect ordering.
- The LC_CTYPE locale specifies which characters are blanks and the LC_NUMERIC locale specifies the thousands separator and decimal-point character. In the C locale, spaces and tabs are blanks, there is no thousands separator, and . is the decimal point.
- -r, --reverse
- Reverse the result of comparison, so that lines with greater key values appear earlier in the output instead of later.
- --random-source=FILE
- Use FILE as a source of random data used to determine which random hash function to use with the -R option.
- -R, --random-sort
- Sort by hashing the input keys and then sorting the hash values. Choose the hash function at random, ensuring that it is free of collisions so that differing keys have differing hash values.
- The choice of hash function is affected by the --random-source option.
- -V, --version-sort
- Sort by version name and number. It behaves like a standard sort, except that each sequence of decimal digits is treated numerically as an index/version number.
- -z, --zero-terminated
- Delimit items with a zero byte rather than a newline (ASCII LF). I.e., treat input as items separated by ASCII NUL and terminate output items with ASCII NUL. This option can be useful in conjunction with perl -0 or find -print0 and xargs -0 which do the same in order to reliably handle arbitrary file names (even those containing blanks or other special characters).
Examples
- Sort a comma separated string ignoring the first char
sed 's/\n//g; s/, */\n/g' <<<'.foo, #bar, -bla' \
| sort --key='1.2' \
| awk '{printf("%s%s", sep, $0); sep=", "} END {print ""}'
Output:
#bar, -bla, .foo
References
- ↑ Wikipedia contributors. "sort (Unix)." Wikipedia. https://en.wikipedia.org/wiki/Sort_(Unix) (accessed 30.07.2024)