Avatar of joaotelles
joaotellesFlag for United States of America 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$VARIABLE.* | grep XXXXX

Is this the easy/fatest way to do this?

How can this for be? - the syntax?

Tks,
Joao
Shell ScriptingProgramming

Avatar of undefined
Last Comment
netcmh

8/22/2022 - Mon
netcmh

bash:

find /path/to/files -type f -mtime -7 -print0 | xargs -0 grep -l XXXXXX
Ben Personick (Previously QCubed)

for j {1...7} do cat TrafficErrorEvents'date+"%Y%m%d"--date="$j days ago"'|grep -e "your grep query"
netcmh

find /path/to/files/TrafficErrorEvents* -type f -mtime -7 -print0 | xargs -0 grep -l XXXXXX
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER
joaotelles

This gave me this output:

> find /var/opt/dpa/log/event/output/TrafficErrorEvents* -type f -mtime -7 -print0 | xargs -0 grep -l TP2
/var/opt/dpa/log/event/output/TrafficErrorEvents20130906.0208
/var/opt/dpa/log/event/output/TrafficErrorEvents20130907.0209
/var/opt/dpa/log/event/output/TrafficErrorEvents20130908.0210
/var/opt/dpa/log/event/output/TrafficErrorEvents20130909.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.
ASKER CERTIFIED SOLUTION
netcmh

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
joaotelles

worked! perfect!

tks!
ASKER
joaotelles

Tks.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
netcmh

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