date from filename

Hi all

I have the following question:

With this command, I get the latest file in a directory.
lastfile =`ls -1tr | tail -n 1`
echo $lastfile

The output is then:
partner131210.txt (meaning 13th December 2010)

My goal is to get the date into a variable and to obtain a final variable that contains the difference in days between today and the date in the file name.

Thanks for your help.

Best regards,
David

To cut date from file name use cut command.

fileDate=`echo partner131210.txt | cut -c8-13`

And this links will help u to find date difference. You should search related forums before posting.
link1 link2 link3

echo partner131210.txt | grep -Eo [0-9][0-9]* 

---------- Post updated at 04:10 PM ---------- Previous update was at 04:08 PM ----------

I doubt we can use cut -c8-13 here because we are not sure about the how many characters are there before date string. Though here date starts from 8th char.

@R0H0N, Will work as long as date is always in 8th-13th position, Won't work if file name itself has some number like abc123131210.txt OR abcdefgabc131210.txt

 
typeset -i a #Date String from the file name
typeset -i b #Day value from the file name
typeset -i c #Day value from current date
typeset -i d #Difference in days
a=$(ls -1tr | tail -n 1 | sed 's/.*\(......\)\..*/\1/')
b=$(echo $a | cut -c1-2)
c=$(date '+%m')
d=$b-$c

May be,

$ lastfile=partner131210.txt
$ expr $lastfile : '.*\(......\).txt'
131210
$