Sorting strings with numbers in Bash [duplicate]

喜你入骨 提交于 2019-11-27 07:03:01
Grzegorz Żur

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)
prefix10
prefix7
prefix4
prefix1

$ printf "prefix%d\n" $(seq 10 -3 1) | sort
prefix1
prefix10
prefix4
prefix7

$ printf "prefix%d\n" $(seq 10 -3 1) | sort --version-sort
prefix1
prefix4
prefix7
prefix10

https://www.gnu.org/software/coreutils/manual/html_node/sort-invocation.html

Try this

$ cat a.txt
abc_1
abc_4
abc_2
abc_10
abc_5

$ sort -V a.txt
abc_1
abc_2
abc_4
abc_5
abc_10
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!