joaotelles
asked on
Shell Script - Syntax question (for)
Hi,
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$VARIABL E.* | grep XXXXX
Is this the easy/fatest way to do this?
How can this for be? - the syntax?
Tks,
Joao
Im writing a shell script but I stuck in a problem:
I have files like this in a directory:
TrafficErrorEvents20130813
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$VARIABL
Is this the easy/fatest way to do this?
How can this for be? - the syntax?
Tks,
Joao
for j {1...7} do cat TrafficErrorEvents'date+"% Y%m%d"--da te="$j days ago"'|grep -e "your grep query"
find /path/to/files/TrafficErro rEvents* -type f -mtime -7 -print0 | xargs -0 grep -l XXXXXX
ASKER
This gave me this output:
> find /var/opt/dpa/log/event/out put/Traffi cErrorEven ts* -type f -mtime -7 -print0 | xargs -0 grep -l TP2
/var/opt/dpa/log/event/out put/Traffi cErrorEven ts20130906 .0208
/var/opt/dpa/log/event/out put/Traffi cErrorEven ts20130907 .0209
/var/opt/dpa/log/event/out put/Traffi cErrorEven ts20130908 .0210
/var/opt/dpa/log/event/out put/Traffi cErrorEven ts20130909 .0211
=====
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.
> find /var/opt/dpa/log/event/out
/var/opt/dpa/log/event/out
/var/opt/dpa/log/event/out
/var/opt/dpa/log/event/out
/var/opt/dpa/log/event/out
=====
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
worked! perfect!
tks!
tks!
ASKER
Tks.
You're welcome. Thanks for the grade. Good luck.
find /path/to/files -type f -mtime -7 -print0 | xargs -0 grep -l XXXXXX