Picking the file based on Date..Requirement

Dear frnds

My requirement is as follows

-rw-r----- 1 f02 dd 109428250 May 18 14:02 Extracts_20070518104730.zip

-rw-r----- 1 f02 dd 109493187 May 21 13:30 Extracts_20070521091700.zip

-rw-r----- 1 f02 dd 109993058 May 23 14:14 Extracts_20070523085955.zip

-rw-r----- 1 f02 dd 110381316 May 25 15:03 Extracts_2007052509491

I recieve the files every alternate days i.e MOn.. Wed...Fri
Now I need a script which receives a parameter (DateSuffix) which will have default value as "AUTO"

Scenario
-------------
So when I run the script on say 21 May and DateSuffix="AUTO", the script should pick up 18 May file.

when I run the script on say 23 May and DateSuffix="AUTO", the script should pick up 21 May file and son on......

The script shud handle teh month and year changes as well.
Any inputs in this regard is appreciated

Thanks in advance
Suresh

I hope you want the file which has created before the execution date. i.e., previous file. So to get it you can use

ls -ltr | tail -2 | head -1 | read Fname

Hi friend,

Thanks for the command
Can we have some other way/method wherein I pick the file based on the datestamp (rather than using head and tail command)

Like for ex: If I run teh process today(i.e MOnday) , I shud grep for the file having date that of previous friday and accordingly pick the file for Friday...

Thanks
Suresh

If GNU date is available you can do :

day_of_week=$(date  +%w)
file_format='Extracts_%Y%m%d*'

if [ $day_of_week -eq 1 ]
then
   file_pattern=$(date --date='last friday' +"$file_format")
else
   file_pattern=$(date --date='yesterday' +"$file_format")
fi

file_name=$(ls $file_pattern 2>/dev/null)

You can also adapt this script to use the datecalc script.

Jean-Pierre.

If you have got GNU date and awk/gawk,

then you can get a backdate with the following command

 var=`date +%s`
awk -v var=${var} -v bdays=2 'BEGIN{print strftime("%d-%m-%Y",var-(86400*bdays))}'

output is 04-06-2007

To get 31 days back date

awk -v var=${var} -v bdays=31 'BEGIN{print strftime("%d-%m-%Y",var-(86400*bdays))}'

Not sure if strftime function is available in Solaris, if it is then this should work on solaris with nawk.

I think that strftime is available only with gawk.

Jean-Pierre.