Link to home
Start Free TrialLog in
Avatar of joaotelles
joaotellesFlag for United States of America

asked on

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?

Avatar of netcmh
Flag of United States of America image


find /path/to/files -type f -mtime -7 -print0 | xargs -0 grep -l XXXXXX
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
Avatar of joaotelles


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.
Avatar of netcmh
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
worked! perfect!

You're welcome. Thanks for the grade. Good luck.