Multiple Field and Numeric Sorting

List of files:

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 

I would like to sort them by mode (rndrd, rndwr, etc.) and then the number:

 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 .... 

I tried the following loop, but it sorted by number, but I need a sequence like 1,4,8,16:

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

EDIT:

Note that numerical sorting (-n) sorts by number (1,1,1,1,4,4,4,4,4 ...), but I need a sequence like 1,4,8,16,1 for example , 4,8,16 ...

+4
source share
2 answers

The for loop is completely unnecessary, as is the -1 argument to ls when passing its output. This gives

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

where the first key begins and ends in column 5, and the second key begins and ends in column 7 and is numeric.

+4
source

Sort by more columns:

 sort -t- -k5,5 -k7n 

Primary sorting - by the 5th column (and not the rest, therefore 5,5 ), secondary sorting by the number in the 7th column.

+5
source

All Articles