Fetch files based on month


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fetch files based on month
# 1  
Old 11-13-2014
Fetch files based on month

Hi All
I have a requirement like below.I have always 12 files got generated in my directory called /xx/out/
Code:
abc_2014_10_121.xml
abc_2014_09_345.xml
abc_2014_08_432.xml
abc_2014_07_123.xml
abc_2014_06_098.xml
abc_2014_05_569.xml
abc_2014_04_430.xml
abc_2014_03_235.xml
abc_2014_02_056.xml
abc_2014_01_450.xml
abc_2013_12_111.xml
abc_2013_11_222.xml

Out of these files I have to select the files based on the below condition and delete the rest of the files.

if it is a JAN month then fetch ----- Jan to Dec = 12 files
if it is a FEB month then fetch ----- Jan + Oct + Nov + Dec = 4 files
if it is a MAR month then fetch ----- Jan + Feb + Oct + Nov + Dec = 5 files
if it is a APR month then fetch ----- Jan + Feb + Mar + Oct + Nov + Dec = 6 files
if it is a MAY month then fetch ----- Apr + Jan + Feb + Mar = 4 files
if it is a JUN month then fetch ----- Apr + May + Jan + Feb + Mar = 5 files
if it is a JUL month then fetch ----- Apr + May + Jun + Jan + Feb + Mar = 6 files
if it is a AUG month then fetch ----- Jul + Apr + May + Jun = 4 files
if it is a SEP month then fetch ----- Jul + Aug + Apr + May + Jun = 5 files
if it is a OCT month then fetch ----- Jul + Aug + Sep + Apr + May + Jun = 6 files
if it is a NOV month then fetch ----- Oct + Jul + Aug + Sep = 4 files
if it is a DEC month then fetch ----- Oct + Nov + Jul + Aug + Sep = 5 files
can any one tell how to achieve this
Regards
Sri

Last edited by Franklin52; 11-13-2014 at 07:37 AM.. Reason: Please use code tags
# 2  
Old 11-13-2014
If you know perl solving this would be a piece of cake imo...
# 3  
Old 11-13-2014
What have you tried?
# 4  
Old 11-13-2014
How about putting some more care and/or effort into formulating the specification?
# 5  
Old 11-13-2014
Hi
Is this homework or some other kind of work? Smilie
# 6  
Old 11-14-2014
I have tried it by extracting the date and doing it by -1,-2.. inside a CASE .
Any better ideas other than this.

Regards
Sri

---------- Post updated at 11:51 PM ---------- Previous update was at 11:47 PM ----------

Hi Ongoto

This is not the homework. For some of the attributes in the files ,updation happens in the later months.That is the reason extracting the previous months files along with the current month.
Regards
Sri
# 7  
Old 11-14-2014
@ chigurupati.dwh
Good. We're clear on that. Smilie

IF statements might be easier to write than CASE.

Your conditions are kinda complicated because they change each month and cycle back every 3 months, so you would need to set different file age limits for each month.

Whadda think about testing each filename in a loop ?
Maybe something like this.
Code:
echo "for month $this_mnth"
echo "files excluded"
LF=$(( $last_file - 1 ))
for x in $(seq 0 "$LF");
do
    y=${files[x]}
    age=${y:9:2} && age=${age#0}
    if [[ $age -lt $this_mnth && $age -lt $lower_limit ]] || \
       [[ $age -gt $this_mnth && $age -lt $upper_Limit ]]; then
        echo $y
    else
        cp $srcdir/$y $fetchdir/
    fi
done

echo "
files fetched"
ls -1 $fetchdir | tr ' ' '\n' | sort -t '_' -k 3

--output--
for month 11
files excluded
abc_2014_01_450.xml
abc_2014_02_056.xml
abc_2014_03_235.xml
abc_2014_04_430.xml
abc_2014_05_569.xml
abc_2014_06_098.xml
abc_2013_12_111.xml

files fetched
abc_2014_07_123.xml
abc_2014_08_432.xml
abc_2014_09_345.xml
abc_2014_10_121.xml
abc_2013_11_222.xml


Last edited by ongoto; 11-14-2014 at 07:51 PM.. Reason: replace bad code with gooder
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Not able to fetch previous month first date and last date

I am not able to fetch first date and last date previous month date -d -1month +%Y-%m-%d date -d -1month +%Y-%m-%d I need two format dd-mm-yyy previous month 01-03-2016 previous month 31-03-2016 and also only date 1 to 31 Aprriciate your replay (4 Replies)
Discussion started by: jagu
4 Replies

2. Shell Programming and Scripting

Fetch the values based on a Key using awk from single file

Hi, Please help to fetch the values for a key from below data format in linux. Sample Input Data Format 11055005|PurchaseCondition|GiftQuantity|1 11055005|PurchaseCondition|MinimumPurchase|400 11055005|GiftCatalogEntryIdentifier|Id|207328014 11429510|PurchaseCondition|GiftQuantity|1... (2 Replies)
Discussion started by: mohanalakshmi
2 Replies

3. UNIX for Dummies Questions & Answers

Need script to move files based on month

Hi , I need a script which moves files based on month. Example : Apr 29 03:16 log4.txt Apr 29 03:16 log5.txt May 4 09:17 log1.txt May 4 09:17 log2.txt Move Apr files into Apr2015(Folder) Move May files into May2015(Folder). This is urgent requirement , if you can help me... (5 Replies)
Discussion started by: rockingvj
5 Replies

4. Shell Programming and Scripting

Fetch parent value based on column values

Hi All, I am trying to achieve the below logic, could you please help me in this. In second row 2nd column I've Value JC2 and the same JC2 is 4th row 1st column.So I need to replace JC2 value in 4th row with JC2 2nd row's 1st column. Input: Job1,JC1 Job1,JC2 Job1,JC3 JC2,JA1... (6 Replies)
Discussion started by: unme
6 Replies

5. Shell Programming and Scripting

Need last month files after 10th of every month

Hi, I need all file names in a folder which has date >= 10th of last month, Example : files in folder AUTO_F1_20140610.TXT BUTO_F1_20140616.TXT CUTO_F1_20140603.TXT FA_AUTO_06012014.TXT LA_AUTO_06112014.TXT MA_AUTO_06212014.TXT ZA_AUTO_06232014.TXT Output: AUTO_F1_20140610.TXT... (9 Replies)
Discussion started by: nani1984
9 Replies

6. Solaris

Move files into different folders based on its month

Hi All, I want to move the files in to different folders based on the files month in the file timestamp. For example All the september files in the directory should moves into the folder "sep_bkp_files" , August files in to aug_bkp_files folder... Please help me to achive the above... (10 Replies)
Discussion started by: velava
10 Replies

7. UNIX for Dummies Questions & Answers

How to fetch files right below based on some matching criteria?

I have a requirement where in i need to select records right below the search criteria qwertykeyboard white 10 20 30 30 40 50 60 70 80 qwertykeyboard black 40 50 60 70 90 100 qwertykeyboard and white are headers separated by a tab. when i execute my script..i would be searching... (4 Replies)
Discussion started by: vinnu10
4 Replies

8. Shell Programming and Scripting

Moving log files based on month - help with ksh shell script

Hello, I'm trying to move the log files from the parent directory to respective monthly folder and I would be running this script on a weekly basis through cron. I'm new to this scripting and here is what i could come up and it runs without really doing anything. I even tried placing echo... (2 Replies)
Discussion started by: jusblues
2 Replies

9. UNIX for Dummies Questions & Answers

how to fetch month format files in a file

I have files like pra.01 sra.02 see.03 swc.03 sre.04 after dot it contain month format when ever user type "01" month i have to fetch what are 01 files to come (2 Replies)
Discussion started by: sgoud
2 Replies

10. Shell Programming and Scripting

How to fetch rows based on line numbers or based on the beginning of a word?

I have a file which will have rows like shown below, ST*820*316054716 RMR*IV*11333331009*PO*40.31 REF*IV*22234441009*xsss471-2762 DTM*003*091016 ENT*000006 RMR*IV*2222234444*PO*239.91 REF*IV*1234445451009*LJhjlkhkj471-2762 </SPAN> DTM*003* 091016 RMR*IV*2223344441009*PO*40.31... (18 Replies)
Discussion started by: Muthuraj K
18 Replies
Login or Register to Ask a Question