To take file based on date passed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To take file based on date passed
# 1  
Old 09-21-2017
To take file based on date passed

Hi Guys,

I have certain files in my directory which gets appended with dates something like this

Code:
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 $file | grep .txt )
done

I need to take only below files based on current month i.e. its sep 17 , i need to take previous named file based on curr_date aug 17

Code:
T1_aug17.txt
T1_Aug17.txt

# 2  
Old 09-21-2017
This is far from clear. Do you need today's day-of-month, one month earlier? Is that always the three-letter-abbreviated month name, or other month representation as well? Case doesn't matter, just for the initial, or the entire string as well? What to do in January?
# 3  
Old 09-21-2017
yes i would need todays day as one month earlier because the curr_date will be always monthly

for eg
Code:
2017-09-11
2017-10-11
2017-10-11

based on the above i need to take files for eg 2017-09-11 then take files of previous month which is Aug17.

The fiiles wil be always in this format that is contains aug17 constant

Code:
T1_aug17.txt/T1_Aug17.txt/T1_aug17_1.txt

# 4  
Old 09-21-2017
So 17 is the year, NOT the day?
# 5  
Old 09-21-2017
Exactly its not day its year
# 6  
Old 09-21-2017
Please look back to your post#1. Where did you state THAT?

Howsoever, try
Code:
DT=$(date +"%b%y" -d"-1month")
ls -1 *.txt | while read FN; do [[ "${FN^^}" =~ "${DT^^}" ]] && echo $FN; done
T1_aug17_1.txt
T1_aug17.txt
T1_Aug17.txt

This User Gave Thanks to RudiC For This Post:
# 7  
Old 09-21-2017
Thanks,

Can this is be done in my existing code

Code:
final_file=$(ls  $path/ | grep -i $file | grep .txt )

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling function, based on agrument passed.

Dears, #!/bin/bash func1() { echo "func1" } func2() { echo "func2" } func3() { echo "func3" } (5 Replies)
Discussion started by: sadique.manzar
5 Replies

2. 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

3. Shell Programming and Scripting

Subtracting months from passed Date

Hi All, I am getting a date from environment variable and want to do some processing by subtracting 2 months from the date passed through the environment variable. I am trying the following syntax : date_var=2014-08-31 date_2M_ago='$date_var+"%d%m%y" --$date_var="2 months ago" '... (3 Replies)
Discussion started by: Rahul Raj
3 Replies

4. 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

5. Shell Programming and Scripting

How to derive the last Sunday's date when Current date value is passed

Hi All, I have a requirement in my project where a batch runs on any day of a week. The input files will land to the server every Sunday. I need to read these files based on the current BUS_DAY (which falls any day of the week). For e.g : if the BUS_DAY is 20120308, we need to derive the... (3 Replies)
Discussion started by: dsfreddie
3 Replies

6. Programming

create a spool file based on values passed from korn shell to sql script

this is my issue. 4 parameters are passed from korn shell to sql script. parameter_1= varchar2 datatype or no value entered my user. parameter_2= number datatype or no value entered my user. parameter_3= number datatype or no value entered my user. parameter_4= number datatype or no... (5 Replies)
Discussion started by: megha2525
5 Replies

7. Shell Programming and Scripting

Incrementing the date depending on the Counter (parameter) passed to the script

Hello Everyone, I have been trying to complete a shell script where, I need to increment the date depending on the file (depending on the date) availability on the remote server. i.e. Basically, I will be passing a counter (like parameter 1 or 2 or 3 or 4). First I will check for the... (1 Reply)
Discussion started by: filter
1 Replies

8. UNIX for Dummies Questions & Answers

Truncating file based on date

Hi, I need to truncate a file based on date.Suppose i have a log file which is getting updated every date,i need to keep 7 days worth of data(like sysdate-7) and rest i want to truncate it.Can some help me? (5 Replies)
Discussion started by: Param0073
5 Replies

9. 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

10. UNIX for Advanced & Expert Users

Delete File Based On Date

Hi Xpert Out There I have a lots of file in this path : -rw-r----- 1 oracle dba 3954176 Dec 21 2006 1_2008.dbf -rw-r----- 1 oracle dba 887808 Dec 21 2006 1_2009.dbf -rw-r----- 1 oracle dba 143872 Dec 21 2006 1_2010.dbf -rw-r----- 1 oracle dba ... (3 Replies)
Discussion started by: adzuanamir
3 Replies
Login or Register to Ask a Question