Extract date from filename and create a new file

Hi, i have a filename CRED20102009.txt in a server

20102009 is the date of the file ddmmaaaa format

the complete route is
/dprod/informatica/Fuentes/CRED20102009.csv

i want to extract the date to create a new file named Parameters.txt

I need to create Parameters.txt with this content in the same /dprod/informatica/Fuentes route

[Comercial.AACPWM001]
$$Date = 20091020 aaaammdd format

how i can do that in unix in a shell, i'm beginner

Thanks

FILENAME="/dprod/informatica/Fuentes/CRED20102009.csv"
DIRNAME=$(dirname $FILENAME)
echo $FILENAME | sed 's/^.*\(..\)\(..\)\(....\)\....$/[Comercial.AACPWM001]\n$$Date = \3\2\1 aaammdd format/' > $DIR/Parameters.txt

The string in your parameters.txt file is just about long enough to fetch the date into another shell var and then cat it out (this also makes it easier to update the parameters format at a later date):

FILENAME="/dprod/informatica/Fuentes/CRED20102009.csv"
DIRNAME=$(dirname $FILENAME)
DATESTR=$(echo $FILENAME | sed 's/^.*\(..\)\(..\)\(....\)\....$/\3\2\1/')
 
cat <<EOF > $DIR/Parameters.txt
[Comercial.AACPWM001]
\$\$Date = $DATESTR aaammdd format
EOF

Try this ..

CompletePath="/dprod/informatica/Fuentes/CRED20102009.csv"
JustFileName=`basename $CompletePath`
JustDate=`echo $JustFileName|awk -F"." '{print $1}'|awk '{print substr($1,length($1)-7, length($1))}'`

Once you get the date alone, you can do the remainder

Hope this helps
Vj

mvijayv OP wanted to swap the date around (see that the year is in the front).

thanks a lot guys, after several intents i can realize it.

dia=`ls /dprod/informatica/fuentes/apolo/Ejem????????.txt|cut -c44-45`
mes=`ls /dprod/informatica/fuentes/apolo/Ejem????????.txt|cut -c42-43`
ano=`ls /dprod/informatica/fuentes/apolo/Ejem????????.txt|cut -c38-41`
fecha="$ano$mes$dia"
var1='[Comercial.AACPWM001]'
var2='$$Date='
var3="$var2$fecha"
echo $var1 > /dprod/informatica/fuentes/apolo/Parameters.txt
echo $var3 >>  /dprod/informatica/fuentes/apolo/Parameters.txt

thanks a lot guys

Great to see your progressing, just be carefull of using cut that way as the numbers 44, 38, etc are very dependant on the length of filename and directory pathes. The sed command I posted earlier cuts the date from the end of the string avoiding these issues.

JustDate=`echo $JustFileName|awk -F"." '{print $1}'|awk '{print substr($1,length($1)-3,length($1))substr($1,length($1)-5,length($1)-10)substr($1,length($1)-7,length
($1)-10)}'`

This should give the date in the format required.
Overlooked that in the beginning. Thx Chubler!