Sorting strings with numbers in Bash [duplicate]
Execute this
sort -t _ -k 2 -g data.file
- -t separator
- -k key/column
- -g general numeric sort
I think this is a GNU extension to sort
, but you're looking for the --version-sort
(or -V
) option:
$ printf "prefix%d\n" $(seq 10 -3 1)prefix10prefix7prefix4prefix1$ printf "prefix%d\n" $(seq 10 -3 1) | sortprefix1prefix10prefix4prefix7$ printf "prefix%d\n" $(seq 10 -3 1) | sort --version-sortprefix1prefix4prefix7prefix10
https://www.gnu.org/software/coreutils/manual/html_node/sort-invocation.html
Try this
$ cat a.txtabc_1abc_4abc_2abc_10abc_5$ sort -V a.txtabc_1abc_2abc_4abc_5abc_10