sort Command Reference for CI Scripts
sort arranges input lines in order, alphabetical by default or numeric on request.
sort is essential for deterministic output (stable diffs, deduped lists). The hidden gotcha is locale: the same command can order differently across runner images unless you pin LC_ALL.
Common flags/usage
- -n / -g: numeric / general-numeric sort
- -u: output unique lines only
- -k FIELD: sort by a key field
- -t CHAR: field separator
- -r: reverse order
- -h: human-numeric (2K, 1G)
Example
shell
LC_ALL=C sort -u allowed-hosts.txt > sorted.txt
du -h --max-depth=1 . | sort -rh | head -n 10
sort -t, -k2 -n data.csvIn CI
Plain sort is locale-sensitive, so a runner with a UTF-8 locale orders case and accents differently than LC_ALL=C, and checked-in "sorted" files fail to compare across machines; pin LC_ALL=C for reproducible byte ordering. Sorting numbers without -n gives 1, 10, 2 (lexicographic). sort -u dedups whole lines.
Key takeaways
- Pin LC_ALL=C for byte-order sorting that is stable across runner images.
- Use -n for numbers; lexicographic order puts 10 before 2.
- sort -u dedups whole lines in one pass without a separate uniq.
Related guides
uniq Command Reference for CI Scriptsuniq filters or counts adjacent duplicate lines in CI. Reference for -c, -d, -u, and -i, plus the must-sort-f…
cut Command Reference for CI Scriptscut extracts columns by field or character in CI. Reference for -f, -d, and -c, plus the single-delimiter and…
awk Command Reference for CI Scriptsawk is a pattern-scanning text processor for CI. Reference for -F field separator, -v variables, and $1/$NF f…
tr Command Reference for CI Scriptstr translates, squeezes, or deletes characters from stdin in CI. Reference for -d, -s, -c, and ranges, plus t…