views:

435

answers:

4

Hi,

I store the SQL script for a particular release in a subdirectory of 'scripts' named after the release version, e.g.

...
./scripts/1.8.3/script-1.8.3.sql
./scripts/1.8.4/script-1.8.4.sql
./scripts/1.8.4.1/script-1.8.4.1.sql
./scripts/1.8.4.2/script-1.8.4.2.sql
./scripts/1.8.4.3/script-1.8.4.3.sql
./scripts/1.9.0/script-1.9.0.sql
./scripts/1.9.1/script-1.9.1.sql
./scripts/1.9.2/script-1.9.2.sql
./scripts/1.9.3/script-1.9.3.sql
./scripts/1.9.4/script-1.9.4.sql
./scripts/1.9.5/script-1.9.5.sql
./scripts/1.9.6/script-1.9.6.sql
./scripts/1.9.6.1/script-1.9.6.1.sql
...

In a bash script, I need to get all the SQL files that apply beyond a certain version number. For example if this version number is 1.9.4 I would like to get the list

./scripts/1.9.4/script-1.9.4.sql
./scripts/1.9.5/script-1.9.5.sql
./scripts/1.9.6/script-1.9.6.sql
./scripts/1.9.6.1/script-1.9.6.1.sql
...

I know I can get the entire list of files ordered by release via

all_files = `find . -name '*.sql' | sort`

But I'm not sure how I can filter this list to get all files "on or after" a particular version.

Thanks, Don

+4  A: 
echo 1.2.3 | awk -F'.' '{ ver=1000000*$1 + 1000*$2 + $3; if (ver > 1002001) print $_ }'
Quassnoi
+1, Nice "version arithmetic" :)
orip
+2  A: 

Brute force (matching patterns with regexps):

find . -name "*.sql" | egrep -v "1\.[0-8]|1\.9\.[0-3]"

Nicer way with sed:

% find . -name "*.sql" | sort -r | sed '/1\.9\.4/ {q}'
...
./scripts/1.9.6/script-1.9.6.sql
./scripts/1.9.6.1/script-1.9.6.1.sql
./scripts/1.9.5/script-1.9.5.sql
./scripts/1.9.4/script-1.9.4.sql

Explanation: sort in reverse, then use sed to stop processing the input the instant the version (1.9.4) is matched.

orip
+1 for the sed, forget the other one
hop
Be careful counting on sort. Even with "-n" version 1.10.14 would be out of order.
Ry4an
+1  A: 

One twist if the files are created ordered by time would be

find . -name \*.sql -newer ./scripts/$VERSION/script-$VERSION.sql -print
Keltia
A: 

Here's a generalized version based on Quassnoi's.

Assume

 $ ls -1 releases/
  program-0.0.1.app
  program-0.0.10.app
  program-0.0.9.app
  program-0.3.1.app
  program-3.3.1.app
  program-3.30.1.app
  program-3.9.1.app

(Notice 0.0.10 comes before 0.0.9, which is wrong). However, if we perform some "version math" we get the correct order:

 $ ls -1 releases/ | sed 's/\(program-\)\(.*\)\(\.app\)/\2 \1\2\3/g' | awk -F '.' '{ ver=1000000*$1 + 1000*$2 + $3; printf "%010d %s\n", ver, $0}' | sort | awk '{print $3}'
 program-0.0.1.app
 program-0.0.9.app
 program-0.0.10.app
 program-0.3.1.app
 program-3.3.1.app
 program-3.9.1.app
 program-3.30.1.app
Nate Murray