How to sort a list of file names by some substring of the name

filenamessort

I have a number of files with names like:

beginning_num1_734_num2_1.363e+12_num3_800.pdf
beginning_num1_735_num2_7.453e+13_num3_800.pdf
beginning_num1_1007_num2_9.453e+12_num3_1200.pdf

I'd like to be able to sort a list of these filenames by the various nums, and pass the sorted list to various command line utilities. For example, if I were sorting by num2, I'd want the order:

beginning_num1_734_num2_1.363e+12_num3_800.pdf
beginning_num1_1007_num2_9.453e+12_num3_1200.pdf
beginning_num1_735_num2_7.453e+13_num3_800.pdf

I have a python script to do this, but it seems like it should be possible with nothing but filters like sed and sort.

Is there any easy way to sort by parts of a file name, using only common command line filters?

Best Answer

GNU sort with the -g(--general-numeric-sort) option should be able to do the trick

sort -k5,5g -t '_'  <<!
beginning_num1_734_num2_1.363e+12_num3_800.pdf
beginning_num1_735_num2_7.453e+13_num3_800.pdf
beginning_num1_1007_num2_9.453e+12_num3_1200.pdf
!
beginning_num1_734_num2_1.363e+12_num3_800.pdf
beginning_num1_1007_num2_9.453e+12_num3_1200.pdf
beginning_num1_735_num2_7.453e+13_num3_800.pdf
Related Question