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