Picking the file based on Date..Requirement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Picking the file based on Date..Requirement
# 1  
Old 06-04-2007
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
# 2  
Old 06-04-2007
I hope you want the file which has created before the execution date. i.e., previous file. So to get it you can use

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

# 3  
Old 06-04-2007
Picking the file based on Date..Requirement

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
# 4  
Old 06-04-2007
If GNU date is available you can do :

Code:
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.
# 5  
Old 06-06-2007
If you have got GNU date and awk/gawk,

then you can get a backdate with the following command

Code:
 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.
# 6  
Old 06-06-2007
I think that strftime is available only with gawk.

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To take file based on date passed

Hi Guys, I have certain files in my directory which gets appended with dates something like this T1_aug17.txt T1_Aug17.txt T1_Sep17.txt config.txt T1 T2 my code: curr_date=`date -d "$date" +%Y-%m-%d` path=mydir for file in `cat config.txt` do final_file=$(ls $path/ | grep -i... (12 Replies)
Discussion started by: rohit_shinez
12 Replies

2. Shell Programming and Scripting

Picking the latest file based on a timestamp for a Dynamic file name

Hi , I did the initial search but could not find what I was expecting for. 15606Always_9999999997_20160418.xml 15606Always_9999999998_20160418.xml 15606Always_9999999999_20160418.xml 9819Always_99999999900_20160418.xml 9819Always_99999999911_20160418.xmlAbove is the list of files I... (4 Replies)
Discussion started by: chillblue
4 Replies

3. HP-UX

HP/UX command to pull file name/date based on date

HI, Can anyone tell me how to pull the date and file name separated by a space using the find command or any other command. I want to look through several directories and based on a date timeframe (find -mtime -7), output the file name (without the path) and the date(in format mmddyyyy) to a... (2 Replies)
Discussion started by: lnemitz
2 Replies

4. Shell Programming and Scripting

File Move based on date

how to move file based on date in linux (1 Reply)
Discussion started by: syedzoniac
1 Replies

5. Shell Programming and Scripting

Script to determine Date,TotalFile,total size of file based on date

I have file listed like below -rw-r--r--+ 1 test test 17M Nov 26 14:43 test1.gz -rw-r--r--+ 1 test test 0 Nov 26 14:44 test2.gz -rw-r--r--+ 1 test test 0 Nov 27 10:41 test3.gz -rw-r--r--+ 1 test test 244K Nov 27 10:41 test4.gz -rw-r--r--+ 1 test test 17M Nov 27 10:41 test5.gz I... (5 Replies)
Discussion started by: krish2014
5 Replies

6. Shell Programming and Scripting

How to call different programs based on requirement?

Hi Team, I have four shell scripts and i need to put it into one script and call based on requirement. For example: If server is Linux and version of my software version is 5 then it should call 1st scipt If server is Solaris and version of my software version is 6 then it should call... (16 Replies)
Discussion started by: darling
16 Replies

7. UNIX for Dummies Questions & Answers

Sorting files based on timestamp and picking the latest file

Hi Friends, Newbie to shell scripting Currently i have used the below to sort data based on filenames and datestamp $ printf '%s\n' *.dat* | sort -t. -k3,4 filename_1.dat.20120430.Z filename_2.dat.20120430.Z filename_3.dat.20120430.Z filename_1.dat.20120501.Z filename_2.dat.20120501.Z... (12 Replies)
Discussion started by: robertbrown624
12 Replies

8. Shell Programming and Scripting

Urgent ...pls Sorting files based on timestamp and picking the latest file

Hi Friends, Newbie to shell scripting. Currently i have used the below to sort data based on filenames and datestamp $ printf '%s\n' *.dat* | sort -t. -k3,4 filename_1.dat.20120430.Z filename_2.dat.20120430.Z filename_3.dat.20120430.Z filename_1.dat.20120501.Z filename_2.dat.20120501.Z... (1 Reply)
Discussion started by: robertbrown624
1 Replies

9. Shell Programming and Scripting

Picking contents value of file at run time based on variable values

HI, I have to write a unix script and need your help. in my application where I have to invoke this script a varialble is there where the value comes in a variable . for example variable can be var=ABC like this there will be any type of value in the vcariable. there is a unix directory... (2 Replies)
Discussion started by: manish8484
2 Replies

10. Shell Programming and Scripting

Split the file based on date value

Hi frnds, I have flat file as . Say : output-file1.txt Output-file2.txt (1 Reply)
Discussion started by: Gopal_Engg
1 Replies
Login or Register to Ask a Question