Fetch files based on month


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fetch files based on month
# 8  
Old 11-14-2014
Following code utilizes the legendary eval command and should be run in the directory containing the xml files.
Code:
#!/bin/bash

#JAN skipped because all files are being kept
FEB=(01 10 11 12)
MAR=(02 "${FEB[@]}")
APR=(03 "${MAR[@]}")
MAY=(01 02 03 04)
JUN=(05 "${MAY[@]}")
JUL=(06 "${JUN[@]}")
AUG=(04 05 06 07)
SEP=(08 "${AUG[@]}")
OCT=(09 "${SEP[@]}")
NOV=(07 08 09 10)
DEC=(11 "${NOV[@]}")

process_date () {
    a=$1[@]

    #assembling appropriate find command (prefix + center + suffix)
    prefix="find . -maxdepth 1 -type f -name '*_*_*.xml' "

     for month in "${!a}"; do
      center="$center! -name '*_*_"$month"_*.xml' "
     done
    
    suffix="-delete"

    findcmd="$prefix$center$suffix"

    echo; echo "$findcmd"
    echo; echo -n "Execute above command?  Y/N "; read yesno

    case "$yesno" in
        [yY]) eval "$findcmd";;
        [nN]) exit;;
           *) echo "Invalid reply."; exit;;
    esac
}

#get abbreviated month name, e.g. NOV
DATE=$(date '+%b' | tr '[:lower:]' '[:upper:]')

case "$DATE" in
        JAN)    echo "It's January!"; exit;;
          *)    process_date $DATE;;
esac

Demo:
Code:
$ ls
abc_2013_11_222.xml  abc_2014_02_056.xml  abc_2014_05_569.xml  abc_2014_08_432.xml  delxml.sh
abc_2013_12_111.xml  abc_2014_03_235.xml  abc_2014_06_098.xml  abc_2014_09_345.xml
abc_2014_01_450.xml  abc_2014_04_430.xml  abc_2014_07_123.xml  abc_2014_10_121.xml
$ ./delxml.sh 

find . -maxdepth 1 -type f -name '*_*_*.xml' ! -name '*_*_07_*.xml' ! -name '*_*_08_*.xml' ! -name '*_*_09_*.xml' ! -name '*_*_10_*.xml' -delete

Execute above command?  Y/N y
$ ls
abc_2014_07_123.xml  abc_2014_08_432.xml  abc_2014_09_345.xml  abc_2014_10_121.xml  delxml.sh
$

This User Gave Thanks to junior-helper For This Post:
# 9  
Old 11-14-2014
I think you meant "infamous".

You can avoid the eval. Never store quotes to parse them later, just store the parameters exactly as you want them -- minus quotes -- in an array or positional parameter. They will expand perfectly without any need to process them again.

If you return to this kind of code in a year, you won't know what it does:
Code:
PREFIX="something"
(10,000 lines)
SUFFIX="somethingelse"
$PREFIX $MIDDLE $SUFFIX

Don't bother stuffing it into $PREFIX and $SUFFIX... They don't change. Just use variables for things that vary, which will leave it as a readable if slightly cryptic find . -type d -maxdepth 1 "$@" -delete which you can figure out without having to find definitions for everything in it first.

-maxdepth is a GNU-only feature by the way.

Code:
     set --
     for month in "${!a}"; do
        set -- "$@" -name "*_*_${month}_*.xml"
     done

     # Change the "-print" to "-delete" once you've tested that it's deleting
     # the right files.
     find . -maxdepth 1 -type f -name '*_*_*.xml' "$@" -print


Last edited by Corona688; 11-14-2014 at 03:46 PM..
This User Gave Thanks to Corona688 For This Post:
# 10  
Old 11-15-2014
Wow!!

This really gets interesting. Smilie
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