Cutting Up a String

I have a file name coming in as such

<string>_YYYYMMDD.DAT

The string could be anything. I want to cut out the date and put it in a variable. Can someone help me with this?

try:

#!/bin/ksh
filename="stuff_20040903.DAT"
filedate=${filename%.*}
filedate=${filedate#*_}
echo $filedate

exit

hi,

you can test with this command :

ls *.dat | sed 's/_/./g'|awk -F . '{print $2}'

the result of this command give you the date.

thanks for your help

Assuming that you have got only DAT extention files to process:

#!/bin/bash

FILES=`ls -1 *DAT`
for file in $FILES
do
ls -1 $file |cut -f2 -d _ | cut -f1 -d . >> date_list
done
DATE_VAR=`cat date_list`