Shell Script - Syntax question (for)


Im writing a shell script but I stuck in a problem:

I have files like this in a directory:

TrafficErrorEvents20130813.0040  TrafficErrorEvents20130813.0099  

I have more than one file for each day - for example of the day 13th I have two...

So, what I need is that every, lets say Friday, I need to do a grep on the files of the last 7 days and get some information..

I was thinking on use a ´for´ statment to increase variable here:

date +"%Y%m%d" --date="1 day ago"

And then a grep like this:

cat TrafficErrorEvents$VARIABLE.* | grep XXXXX

Is this the easy/fatest way to do this?

How can this for be? - the syntax?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


find /path/to/files -type f -mtime -7 -print0 | xargs -0 grep -l XXXXXX
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
for j {1...7} do cat TrafficErrorEvents'date+"%Y%m%d"--date="$j days ago"'|grep -e "your grep query"
find /path/to/files/TrafficErrorEvents* -type f -mtime -7 -print0 | xargs -0 grep -l XXXXXX
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

joaotellesAuthor Commented:
This gave me this output:

> find /var/opt/dpa/log/event/output/TrafficErrorEvents* -type f -mtime -7 -print0 | xargs -0 grep -l TP2


But I wanted to have the lines that have TP2 in the files...

I mean something like if the first file have 3 lines with TP2 and the second have another 3, I would like have the 6 lines as output.
try this

find /var/opt/dpa/log/event/output/TrafficErrorEvents* -type f -mtime -7 -print0 | xargs -0 grep TP2

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
joaotellesAuthor Commented:
worked! perfect!

joaotellesAuthor Commented:
You're welcome. Thanks for the grade. Good luck.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.