Linux Shell sort命令,多域排序的正确用法。。

之前多少次都是错的。

这才是正确的,假设对列1按照字母序,2按照数值序。

sort -k 1,1 -k2,2n

2014.05.12更新:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
-g, --general-numeric-sort compare according to general numerical value
-n, --numeric-sort compare according to string numerical value
-g, --general-numeric-sort compare according to general numerical value -n, --numeric-sort compare according to string numerical value
  -g, --general-numeric-sort  compare according to general numerical value
  -n, --numeric-sort          compare according to string numerical value

如果数据中有科学计数法,形如"8.0177E-4",默认是不支持它排序的,可以用-g代替-n

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sort -k2,2g
sort -k2,2g
sort -k2,2g

2015.02.25更新:

如果要制定分割符为tab键,可以如下:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sort -t$'\t' -k3 -nr file.txt
sort -t$'\t' -k3 -nr file.txt
sort -t$'\t' -k3 -nr file.txt

 

Leave a Reply

Your email address will not be published. Required fields are marked *