List files sorted numerically in bash

enthuguy
enthuguy used Ask the Experts™
on
hi friends,
how to sort files by extension by numeric portion in bash please?

e.g I have below below files
file.name.XMLSchema10
file.name.XMLSchema46
file.name.XMLSchema47
file.name.XMLSchema7
file.name.XMLSchema469
file.name.XMLSchema470
file.name.XMLSchema471
file.name.XMLSchema472
file.name.XMLSchema273
file.name.XMLSchema374
file.name.XMLSchema475
file.name.XMLSchema576
file.name.XMLSchema177
file.name.XMLSchema178
file.name.XMLSchema179
file.name.XMLSchema180
file.name.XMLSchema181
file.name.XMLSchema182
file.name.XMLSchema183
file.name.XMLSchema184
file.name.XMLSchema185
file.name.XMLSchema186
file.name.XMLSchema187
file.name.XMLSchema188
file.name.XMLSchema189
file.name.XMLSchema90
file.name.XMLSchema491
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Thx wollmilkporc for your quick help.

not getting the output in order. Is it possible to get in asec (lowest at the top) pls
Most Valuable Expert 2013
Top Expert 2013

Commented:
Sorry, I noticed myself that my solution doesn't work - that's why I deleted my comment.

Let's see if  I can find a better solution!

Author

Commented:
Thanks very much :)
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Most Valuable Expert 2013
Top Expert 2013
Commented:
There is a GNU extension to "ls" which should do what you desire:

ls -1 -v file.name.*

Please note: "-1" is the digit "1", not the letter "l". We use it here to force one-column output.

Working on a solution with filenames in a file ...

Author

Commented:
Thx very much that helped! :)
for file in file.name.XMLSchema{1..1000}; do
    ls "$file"
done

Author

Commented:
thx mankowitz, I believe this will list up to file.name.XMLSchema1000 even if the actual file "file.name.XMLSchema1000" doesn't exist?

file.name.XMLSchema1
file.name.XMLSchema2
.
.
.
file.name.XMLSchema1000

pls correct me if I was wrong
it shouldnt. If the file is not there, ls should return empty.

Author

Commented:
oh Yeah, I was wrong...sorry

thanks
Most Valuable Expert 2013
Top Expert 2013

Commented:
A possible solution for sorting a list of files "filelist":

sed 's/\(file\.name\.XMLSchema\)\(.*$\)/\1 \2/' filelist | sort -k2n | tr -d " "

The file names must not contain spaces for this solution to work!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial