Remove data from file

Hi

I have file with filename as below and need to remove based on date in it.

Data in file:

filename_092013.csv
filename_082013.csv
filename_072013.csv

I want to remove filenames which are generated 15 days back. where date is generated in file. I need to create another file with filename which are created in last 15 days.

Hi,

Could you please provide more information on your date formate
(like . DDMMYYY or something else ) ?
Please check filename, provided in your first post and let us know.

Thanks
Pravin

Hi Pravin,

Date format is MMDDYY.
filename_<MMDDYY>.csv

What i want to do is I just want a file with last 15 days of files. rest should be removed from file.

Please let me know if you need any more information.

Thanks

Remove files older than 15 days from the current directory

find . -type f -name '*.csv' -mtime +15 -exec echo rm -f {} \;

It uses the file modification time as shown by ls -t - not the date in the filename.
If you think it lists the correct rm commands, remove the echo to really run it.

Thanks

But I dont want to remove files in the directory. I have list of filenames in one file. all file names should be removed based on date in filename.

Please explain in more detail.

I think MadeInGermany has answered your question.

What are you trying to do?

Hi hicksd8

What i am trying to do is I have one text file(Eg:Filelist.txt) It has list of filenames(Filenames are contained with date as mentioned in the initial post)

I want remove the filenames from the Filelist.txt file and create new file with valid filenames

Validation Process: all filenames should not be belongs to not more than 15 days.

Note: Not from the filesystem files we are deleting filenames from one text file and creating new file.

Exactly 15 days, or older than 15 days?

If it's exactly 15 days (and you have GNU date) you could just do:

grep -v $(date +%m%d%y --date '15 days ago') filelist.txt

EDIT:
For a range you could generate the grep regex:

dates=$(date +%m%d%y)
for i in {1..15}
do
   dates="${dates}|"$(date +%m%d%y --date "$i days ago")
done
grep -E "${dates}" filelist.txt > newfilelist.txt

That's bash with GNU date and GNU grep - you may need to fiddle with it if you don't have them.

If +/- 2 days do not matter:

awk -F '[_.]' 'function mmddyy(x){return (substr(x,1,2)*30+substr(x,3,2)+substr(x,5,2)*365)} mmddyy($2)<mmddyy(date)-15' date=`date +%m%d%y` Filelist.txt

More precise is the previous proposal (calculation in GNU date)

awk -F '[_.]' 'function mmddyy(x){return (substr(x,1,2)*30+substr(x,3,2)+substr(x,5,2)*365)} mmddyy($2)<mmddyy(date)' date=`date --date '15 days ago' +%m%d%y` Filelist.txt

If you think it works for you, redirect the output to a new file with >Filelist.new