CLI tip 10: version sort
You can use
sort -V for sorting numerical input that is mixed with other characters. It also helps when you want to treat digits after a decimal point as whole numbers, for example if
1.10 should be greater than
$ printf '1.5\n1.10\n1.2' | sort -n 1.10 1.2 1.5 $ printf '1.5\n1.10\n1.2' | sort -V 1.2 1.5 1.10 $ cat versions.txt file2 cmd5.2 file10 cmd1.6 file5 cmd5.10 $ sort -V versions.txt cmd1.6 cmd5.2 cmd5.10 file2 file5 file10
Here's an example of dealing with numbers reported by the
time command (assuming all the entries have the same format).
$ cat timings.txt 5m35.363s 3m20.058s 4m11.130s 3m42.833s 4m3.083s $ sort -V timings.txt 3m20.058s 3m42.833s 4m3.083s 4m11.130s 5m35.363s
See GNU coreutils manual: Version sort ordering for more details. Also, note that the
ls command uses lowercase
-v for this task.
See sort command chapter from my Command line text processing with GNU Coreutils ebook for more details.