values.txt contains 2, 10, and 100 on separate lines.
Sort them by numeric value in ascending order rather than lexicographically.
Which command is appropriate?
sort -n compares lines as numbers. Add -r when reverse numeric order is needed.
Detailed explanation
sort -n values.txtCorrect. sort -n compares the values numerically.
sort values.txtIncorrect. Default sort uses string comparison, which can place 10 before 2.
sort -r values.txtIncorrect. -r reverses the comparison but does not enable numeric comparison.
uniq values.txtIncorrect. uniq removes adjacent duplicates and does not sort values.
Try it yourself
An example you can run in a temporary verification environment.
printf '10\n2\n100\n' | sort -nExpected result
2
10
100Key points
- -n means numeric
- Default sort is lexical
- -r reverses the result
Notes
- Environment: GNU coreutils 9.x / 標準入力
- Use small, disposable text streams when testing filters.
- Locale, encoding, and GNU/POSIX implementation details can affect edge cases.
Foundation review
Place the operation
Numeric sort prevents lexical ordering from placing values such as 10 before 2.
Check the side effect
Use a small list whose lexical and numeric orders differ.