How to sort the files according to the number?

Hi Everyone,

I have a question:
I have a lot of file named like
or10000.dat, or9100.dat, or100.dat, or3100.dat...
I want to deal with these files according to the
number in the name. So I want to deal with or100.dat
first and then or3100.dat and so on.

I used :

for i in `ls or*.dat | sort -n`;do ...

but the first one is or 10000.dat.

Is there an easy way to do that?
Thanks in advance!

/YC

Use sort -k through which you can specify the starting and ending position of your sort criteria.

So in your case

ls or*.dat | sort -k2 -n

---------- Post updated at 03:50 AM ---------- Previous update was at 03:48 AM ----------

Sorry..It should be -k3

try:

for i in `ls or*.dat | sort -k1.3n`;do

Some sorts allow a --debug option which is good to see what they are sorting on.

Thank you. I tried that. but the file of or10000.dat is still
in front of or100.dat.
it seems not to work.

Yes my solution should work

Yes Chubler_XL is correct..should use 'k.'

ls or*.dat | sort -t' ' -k1.3 -n
or100.dat
or9100.dat
or10000.dat

Yes, this one works. But I don't know why even after I read
the man page of sort. There is no "-k.". Would you please
explain why this one works? thank you.

by the way I tried to use --debug, but I got unrecognized option '--debug'

What OS are you on? all the ones I know have -k explained in manpages...

Yes, I have -k in man page. but it is
-k, --key=POS1[,POS2]
start a key at POS1, end it at POS2 (origin 1)

there is no explanation for "-k.". It is comma, not dot.
thank you.

Usually further down in the page POS are explained further:

Check the man entries on this site they are quite good ( Linux Sort)

You may have an outdated manual page, -k is the modern way to tell sort to sort on a column. -k 1 would tell it to start at the first column (and consider all the columns thereafter), -k 1,1 would tell it to consider only the first column, etc. The options for it are quite complex.

Well, in the old days 'sort -k3,5' was 'sort +2 -4'!