1

This was similar to BASH: how to loop all files in sorted order but different enough I felt-

I have a series of files prepended with pound sign and number that in bash by default get ordered like this:

#1 Clear.png
#10 Brilliant Blue.png
#11 Caribbean Blue.png
#12 Aquamarine.png
#13 Teal.png
#14 Turquoise.png
#15 Green.png
#16 Light Green.png
#17 Purple.png
#18 Lt Purple.png
#19 Yellow.png
#2 Standard Pink.png
#20 Brown.png
#3 Light Pink.png
#4 Fuschia.png
#5 Wine.png
#6 Red.png
#7 Orange.png
#8 Blue.png
#9 Light Blue.png

Id like to sort these in a for loop by their numeric value in front of the pound sign in this order i.e. something like

for i in `ls * | sort [method to sort]`; do
*stuff*
done

The issue is that none of the sorting solutions I have found seem readily usable for sorting this case with a pound sign and/or spaces.

Order files should be sorted:

#1 Clear.png
#2 Standard Pink.png
#3 Light Pink.png
#4 Fuschia.png
#5 Wine.png
#6 Red.png
#7 Orange.png
#8 Blue.png
#9 Light Blue.png
#10 Brilliant Blue.png
#11 Caribbean Blue.png
#12 Aquamarine.png
#13 Teal.png
#14 Turquoise.png
#15 Green.png
#16 Light Green.png
#17 Purple.png
#18 Lt Purple.png
#19 Yellow.png
#20 Brown.png
morganwebdev
  • 170
  • 11

1 Answers1

1

sort -k 1.2 -n should do the trick

-k F.C defines that input should be sorted according to field F, starting at character C. Both starting at 1

Edit: Just now I realize that my answer is pretty much the same answer to the question you linked. So definitely a duplicate

Fred
  • 1,462
  • 8
  • 15
  • I think its different enough from the one I linked but maybe not from the one marked as duplicate which I hadn't seen. Thank you I think this is the answer anyways – morganwebdev Mar 12 '18 at 18:15
  • You can add the n (numeric) in the keydef : sort -k1.2n – ctac_ Mar 12 '18 at 18:29