user49204
user49204

Reputation: 381

Numerical sort of file names

I have a folder with scripts with a name pattern of UPDATE[x.y.z] where x.y.z is the script's version.

What I need is a bash script that runs the scripts ordered by their version, hence alphabetic sort is not good.

For example UPDATE1.11.0 should be executed after UPDATE1.2.3.

is there a comparator I can use on order to dictate that sorting order? if not, how else can it be done?

Upvotes: 2

Views: 124

Answers (2)

ghoti
ghoti

Reputation: 46826

And if you don't have GNU sort (i.e. you're in OSX or FreeBSD or NetBSD), you may be able to fake it by sorting different fields.

[ghoti ~]$ printf "foo1.2.3\nfoo1.11.0\nfoo1.4.1\nfoo1.4.0\n" | sort -nt. -k2,3
foo1.2.3
foo1.4.0
foo1.4.1
foo1.11.0
[ghoti ~]$

This misses out on the major version number because it's not delimited by a dot. But it may work for you anyway.

Upvotes: 1

Dennis Williamson
Dennis Williamson

Reputation: 359965

If you have GNU sort or another version that supports it, you can use version sort.

sort -V

or

sort --version-sort

Also, in my answer here, I posted a script which compares versions.

Upvotes: 1

Related Questions