Sub String in UNIX Script

Hi

I have filenames coming as

FILE.V<Version>.YYYYMMDD.z

FILE.V260.20140423.z
FILE.V1.20140523
FILE.V9999.20140324.z

How do I extract the version number in a variable

I need

260
1
9999

extracted into variable
by Unix script

Welcome to forums,

One way to do so

$ file="FILE.V260.20140423.z"
$ string=$(cut -d'.' -f2 <<<$file |  sed 's/[[:alpha:]]//g')
$ echo $string
260

for multiple

for file in FILE.*; do
        string=$(cut -d'.' -f2 <<<$file |  sed 's/[[:alpha:]]//g')
        echo $string
done

if here string is not supported then replace

string=$(cut -d'.' -f2 <<<$file | sed 's/[[:alpha:]]//g')

with

string=$(echo $file | cut -d'.' -f2 | sed 's/[[:alpha:]]//g' )

or

string=$(echo $file | awk -F'.' '{sub(/[[:alpha:]]/,x,$2);print $2}'

---------- Post updated at 12:07 PM ---------- Previous update was at 11:43 AM ----------

Or else try this

$ file=FILE.V260.20140423.z

$ str1="${file#"${file%%[[:digit:]]*}"}"

$ str2="${str1%%[^[:digit:]]*}"

$ echo $str2
260

For any shell that performs POSIX standard variable expansions (such as bash and ksh ), you could also try:

#!/bin/ksh
for file in FILE.V*
do	version="${file#FILE.V}"
	version="${version%%.*}"
	printf "File %s version is %s\n" "$file" "$version"
done

If the files listed in the 1st post in this thread are present in the directory where this script is run, it will produce the output:

File FILE.V1.20140523 version is 1
File FILE.V260.20140423.z version is 260
File FILE.V9999.20140324.z version is 9999

Note that this script only uses shell built-ins so it should be more efficient than the scripts Akshay suggested (which invoke cut and sed , or awk depending on which suggestion you use).

vers=`expr "$file" : '[^.][^.]*\.V\([^.][^.]*\)'`

old school... should work everywhere pretty much.
Feel free to replace backticks with $(...) which is pretty much everywhere now.. like

vers=$(expr "$file" : '[^.][^.]*\.V\([^.][^.]*\)')

Arguably if you have $(..) (e.g. bash or ksh), then you might like some of the other solutions better.