Extracting Date from string

Hi Gurus
I want to extract a date and version code which shall come in filename consisting of underscores.
The filename can contain any / one underscores but the version number will come after date and will be separted by underscore
String formats

ABC_20090815_2.csv
ABC_DEF_20090815_2.csv
ABC_DEF_GH_20090815_2.csv
ABC_DEF_G_I_20090815_2.csv
ABC_DEF_M_N_O_P_20090815_2.csv

In each case i want to extract
date -- 20090815 and
version number -- 2

Try

$ echo ABC_DEF_M_N_O_P_20090815_2.csv | awk -F '[_\.]' '{ print "date: ",$(NF-2), "\nversion: ",$(NF-1) }'

sed version :

sed 's/.*_\(.*\)_\(.*\).csv/\1 "version" \2/' file_names_file

There's no need for any external command. Use the shell:

IFS=_.
for file in *.csv
do
  set -- $file
  shift $(( $# - 3 ))
  echo "date=$1"
  echo "version=$2"
done

Thanks for all your replies .. just one more scenario ..

The file name can end without .csv as well

ABC_20090815_2
ABC_DEF_20090815_2
ABC_DEF_GH_20090815_2
ABC_DEF_G_I_20090815_2
ABC_DEF_M_N_O_P_20090815_2

still how to extract date and version

Thanks much

Should be suffice

while read file
do
echo "$file Date and Ver: `echo $file | grep -o '[0-9]\{8\}_[0-9]'`"
done < file

just play with the code for formatting

Hey, just modify panyam's command a bit and it's done:

sed 's/.*_\(.*\)_\(.*\)/\1 version \2/' yourfile

There's still no need for an external command.

IFS=_
set -- $file
shift $(( $# - 2 ))
echo "date=$1"
echo "version=$2"

this is throwing error shift out of range

line 3: shift: -2: shift count out of range
date=
version=

What is the value of $file?

the file values are

DLR1JPCHASE_All_Trades_Report_20090630_2.csv
GCR_20090630_2.csv
GCR1_20090630_2.csv
b_20090630_2.csv
c_20090630_2
pqr_20090630_2

in each case we need to extract 20090630_2 i.e date 20090630 and version number : 2

IFS=_
set -- ${file%.csv}
shift $(( $# - 2 ))
echo "date=$1"
echo "version=$2"

Its still throwing

./filename: line 3: shift: -2: shift count out of range

I just need to extract date 20090630 adn version code 2 from the following format ( files with .csv extension and without aswell )

(with extension )
DLR1JPCHASE_All_Trades_Report_20090630_2.csv
GCR_20090630_2.csv
GCR1_20090630_2.csv
b_20090630_2.csv

( without extension)
c_d_e_f20090630_2
pqr_d_20090630_2
pqr_20090630_2

Any help will be highly appreciated

I repeat: What is the value of "$file"?

I don't want the names of the files; I want the actual value of the variable.

(You do realize that the code I posted was to operate on a single file, don't you? To work on all the .csv files, you need to put it in a loop with all of the file names. Use whatever pattern finds them all.)