change date format and then compare dates

I have filenames
filenameA_fg_MMDDYY.tar.gz
filenameASPQ_fg_MMDDYY.tar.gz
filenameAFTOPHYYINGH_fg_MMDDYY.tar.gz
filenameAGHYSW_fg_MMDDYY.tar.gz

My requiremnt needs to extract date which is in MMDDYYand change it into YYYYMMDD format.

I have done the following:
filedate=`echo $filename | awk -F"[_.]" '{print $(NF-2)}'`
formatdate = `echo $filedate | awk 'BEGIN {OFS=""}{print 20 substr($1,5,2), substr($1,1,2), substr($1,3,2)}'`

if [ formatdate -lt myowndate ]

This comparison doesnot work . I have tried putting quotes, < everything nothing works. I understand the problem is formatdate datetype is actually string and so it is not doing the compare.

Is there a way where I can change the MMDDYY date to YYYYMMDD number format . Basically what i need is the YYYYMMDD format to be number datatype . Any solution . I apologise if im not clear.:(:D:mad::o

to give you a head start

-bash-3.2$ date +"%Y%m%d"
20090724
-bash-3.2$ date +"%m%d%y"
072409
-bash-3.2$

hi ryanthegr8,

I cannot use DATE command . I have to extract the date out of the filenames like
071609 and change it to 20090716. Change is not a problem but I need 20090716 in number format. Does it make sense?sorry again if im not clear. And thanks though

try this

-bash-3.2$ file='filenameAGHYSW_fg_050509.tar.gz'
-bash-3.2$ filedate=`echo ${file//[^0-9]/}`
-bash-3.2$ year=`echo $filedate | sed 's|^....||'`
-bash-3.2$ monthday=`echo $filedate |sed 's|..$||g'`
-bash-3.2$ timestamp=`echo "20${year}${monthday}"`
-bash-3.2$ echo $file | sed "s|$filedate|$timestamp|"
filenameAGHYSW_fg_20090505.tar.gz
-bash-3.2$

Hi RubinPat

you can try this one

filename=filenameA_fg_072409.tar.gz
fileformatdate=`echo $filename | cut -d "_" -f3 | cut -d "." -f1`
year=20`echo $fileformatdate | cut -c 5-6`
mmday=`echo $fileformatdate | cut -c 1-4`
reqformateddate=`echo $year$mmday`
#!/bin/ksh
myowndate="$1"
shift
for fname in $*
do
    # remove all before last _ including _
    step1=${fname##*_}
    # remove all after first . including .
    mmddyy=${step1%%.*}
    yyyymmdd="20${mmddyy:4:2}${mmddyy:0:2}${mmddyy:2:2}"
    if (( yyyymmdd < myowndate )) ; then
        echo "$fname"
    fi
done

using ex.

./thisscript 20081130 filename*_*tar*

---------- Post updated at 10:41 AM ---------- Previous update was at 10:35 AM ----------

your "about" scriptlines include some syntax error.
No space using set value for variable:
formatdate=`echo ...

Using variable, need $

if [ "$formatdate" -lt "$myowndate" ] ; then
   ...
fi