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 $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
T1_aug17.txt
T1_Aug17.txt
RudiC
September 21, 2017, 3:04am
2
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?
yes i would need todays day as one month earlier because the curr_date will be always monthly
for eg
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
T1_aug17.txt/T1_Aug17.txt/T1_aug17_1.txt
RudiC
September 21, 2017, 3:16am
4
So 17 is the year, NOT the day?
Exactly its not day its year
RudiC
September 21, 2017, 3:35am
6
Please look back to your post#1. Where did you state THAT?
Howsoever, try
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
1 Like
Thanks,
Can this is be done in my existing code
final_file=$(ls $path/ | grep -i $file | grep .txt )
RudiC
September 21, 2017, 3:48am
8
Why don't you try it and present the resulting code here for analysis?
I have added something like this
DT=$(date +"%b%y" -d"-1month")
final_file=$(ls $path/ | grep -i $file$DT | grep .txt )
RudiC
September 21, 2017, 4:17am
10
And - does it come close to something like you want?
Almost but it's searching pattern like filenameaug17. Can you help where is the mistake
RudiC
September 21, 2017, 12:40pm
12
No, it doesn't:
touch filenameaug17.
ls -1 *.txt | while read FN; do [[ "${FN^^}" =~ "${DT^^}" ]] && echo $FN; done
T1_aug17_1.txt
T1_aug17.txt
T1_Aug17.txt
With your approach, try to make the ls
command or the regex more specific
Hi,
I didn't get how it will change , because grep $file$DT will search for filenameAug17 pattern.
DT=$(date +"%b%y" -d"-1month")
final_file=$(ls $path/ | grep -i $file$DT | grep .txt )
Are you suggesting something like this
DT=$(date +"%b%y" -d"-1month")
final_file=$(ls $path/ | grep -i $file| grep | $DT| grep .txt )