2013-04-30 70 views
4

文件列表:多個領域和數字排序

sysbench-size-256M-mode-rndrd-threads-1 
sysbench-size-256M-mode-rndrd-threads-16 
sysbench-size-256M-mode-rndrd-threads-4 
sysbench-size-256M-mode-rndrd-threads-8 
sysbench-size-256M-mode-rndrw-threads-1 
sysbench-size-256M-mode-rndrw-threads-16 
sysbench-size-256M-mode-rndrw-threads-4 
sysbench-size-256M-mode-rndrw-threads-8 
sysbench-size-256M-mode-rndwr-threads-1 
sysbench-size-256M-mode-rndwr-threads-16 
sysbench-size-256M-mode-rndwr-threads-4 
sysbench-size-256M-mode-rndwr-threads-8 
sysbench-size-256M-mode-seqrd-threads-1 
sysbench-size-256M-mode-seqrd-threads-16 
sysbench-size-256M-mode-seqrd-threads-4 
sysbench-size-256M-mode-seqrd-threads-8 
sysbench-size-256M-mode-seqwr-threads-1 
sysbench-size-256M-mode-seqwr-threads-16 
sysbench-size-256M-mode-seqwr-threads-4 
sysbench-size-256M-mode-seqwr-threads-8 

我謹模式(rndrd,rndwr等)對它們進行排序,然後數:

sysbench-size-256M-mode-rndrd-threads-1 
sysbench-size-256M-mode-rndrd-threads-4 
sysbench-size-256M-mode-rndrd-threads-8 
sysbench-size-256M-mode-rndrd-threads-16 
sysbench-size-256M-mode-rndrw-threads-1 
sysbench-size-256M-mode-rndrw-threads-4 
sysbench-size-256M-mode-rndrw-threads-8 
sysbench-size-256M-mode-rndrw-threads-16 
.... 

我已經試過了下面的循環,但它是由數排序,但我需要的序列,例如1,4,8,16:

$ for f in $(ls -1A); do echo $f; done | sort -t '-' -k 7n 

編輯:

請注意,數字排序(N)的數量對其進行排序(1,1,1,1,4,4,4,4 ...),但我需要一個像1,4,8,16,1序列,4,8,16 ......

回答

4

for循環是完全不必要的,因爲-1參數爲ls管道輸出。收益率爲

ls -A | sort -t- -k 5,5 -k 7,7n 

其中第一個鍵在第5列開始和結束,第二個鍵在第7列開始和結束並且是數字。

5

排序列:

sort -t- -k5,5 -k7n 

主排序是第5列(而不是休息,這就是爲什麼5,5),在第7欄按號碼排序中學。

+0

該命令被接受,但似乎悄悄忽略'N'(使用GNU排序8.20) – msw 2013-04-30 11:50:07

+0

@msw:哎呀,我的錯誤。我mislpaced了'N'! – choroba 2013-04-30 11:52:35