bash 最后按数字对字符串列表进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16945134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash sort list of strings by number at the end
提问by Sam Brinck
I have a list of files with a version number at the end that I need to sort
我有一个文件列表,最后有一个我需要排序的版本号
/this/is/a/file/path/product-2.0/file/name/7
/this/is/a/file/path/product-2.0/file/name/10
/this/is/a/file/path/product-2.0/file/name/12
/this/is/a/file/path/product-2.0/file/name/13
/this/is/a/file/path/product-2.0/file/name/6
/this/is/a/file/path/product-2.0/file/name/8
/this/is/a/file/path/product-2.0/file/name/9
when I pipe it through grep it sort like so:
当我通过 grep 管道它时,它是这样排序的:
echo $files | sort -n
/this/is/a/file/path/product-2.0/file/name/10
/this/is/a/file/path/product-2.0/file/name/12
/this/is/a/file/path/product-2.0/file/name/13
/this/is/a/file/path/product-2.0/file/name/6
/this/is/a/file/path/product-2.0/file/name/7
/this/is/a/file/path/product-2.0/file/name/8
/this/is/a/file/path/product-2.0/file/name/9
I think the -n is getting confused by the first number in the file name.
我认为 -n 被文件名中的第一个数字弄糊涂了。
How can I sort it numerically by the last number
如何按最后一个数字对它进行数字排序
回答by nitin
Kaizen ~/so_test $ cat ztestfile1 | sort -t'/' -n -k10
/this/is/a/file/path/product-2.0/file/name/6
/this/is/a/file/path/product-2.0/file/name/7
/this/is/a/file/path/product-2.0/file/name/8
/this/is/a/file/path/product-2.0/file/name/9
/this/is/a/file/path/product-2.0/file/name/10
/this/is/a/file/path/product-2.0/file/name/12
/this/is/a/file/path/product-2.0/file/name/13
does this help ?
这有帮助吗?
alternative way ie to be independent from the position .....
另一种方式,即独立于位置.....
Kaizen ~/so_test $ cat ztestfile1 | sort -V
/this/is/a/file/path/product-2.0/file/name/6
/this/is/a/file/path/product-2.0/file/name/7
/this/is/a/file/path/product-2.0/file/name/8
/this/is/a/file/path/product-2.0/file/name/9
/this/is/a/file/path/product-2.0/file/name/10
/this/is/a/file/path/product-2.0/file/name/12
/this/is/a/file/path/product-2.0/file/name/13
please note its a -V (Capital) option in sort that looks into numeric differences in a string to sort ..... like in version number .
请注意它的排序中的 -V(大写)选项,它查看字符串中的数字差异以进行排序......就像在版本号中一样。
man page text ::
手册页文本 ::
-V, --version-sort
natural sort of (version) numbers within text
回答by michas
You can use /as a column separator:
您可以/用作列分隔符:
sort -n -t/ -k 10
回答by iruvar
Use awkto bring the last field to the front, sortby the 1st field, then apply cutto get rid of the first field. An example using a here documentbelow
使用awk带来的最后场前,sort由第一场,然后应用cut摆脱了第一场。使用here document下面的示例
awk -F'/' '{print($NF"/"ruby -we 'print *readlines.sort_by{|x| File.basename(x).to_i}'
)}' <<! | sort -k1n,1n -t'/' | cut -f2- -d'/'
> /this/is/a/file/path/product-2.0/file/name/7
> /this/is/a/file/path/product-2.0/file/name/10
> /this/is/a/file/path/product-2.0/file/name/12
> /this/is/a/file/path/product-2.0/file/name/13
> /this/is/a/file/path/product-2.0/file/name/6
> /this/is/a/file/path/product-2.0/file/name/8
> /this/is/a/file/path/product-2.0/file/name/9
> !
/this/is/a/file/path/product-2.0/file/name/6
/this/is/a/file/path/product-2.0/file/name/7
/this/is/a/file/path/product-2.0/file/name/8
/this/is/a/file/path/product-2.0/file/name/9
/this/is/a/file/path/product-2.0/file/name/10
/this/is/a/file/path/product-2.0/file/name/12
/this/is/a/file/path/product-2.0/file/name/13
回答by michas
Or use a one-liner something like perl or ruby:
或者使用像 perl 或 ruby 这样的单行代码:
cat data | awk 'BEGIN { FS="/"} { print $(NF) " " ##代码## }' | sort -n | sed 's/^.*\s//g'
回答by kgraney
This gets you sorted by the last item in the path by using /as the field separator, taking the last value and placing it at the start of the line, sorting, then removing the value from the start of the line.
这使您可以使用/作为字段分隔符的路径中的最后一项进行排序,取最后一个值并将其放在行的开头,排序,然后从行的开头删除该值。

