Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to output the sum of a grep results for a certain time, for now i hard coded the time grep but any help will be very much appreciated, my problem is i cant output the results on the script.

If i use this line, it will give me an output of 1:

cat *.log | egrep '2011-07-01 (04:[0][0-9]:[0-5][1-9]|04:10:00)' * | grep -ach '0110 478655 .. 51' | awk '{SUM += $1} END { print SUM }'

and this line would give me an output of 0 : cat *.log | egrep '2011-07-01 (04:[1][0-4]:[0-5][1-9]|04:15:00)' * | grep -ach '0110 478655 .. 51' | awk '{SUM += $1} END { print SUM }'

this code needs alot of improvements :

while read line
cd $DIR
ta1=`cat *.log | egrep '2011-07-01 (04:[0][0-9]:[0-5][1-9]|04:10:00)' * | grep -ach '0110 478655 ..  51' | awk '{SUM += $1} END { print SUM }'`
ta2=`cat *.log | egrep '2011-07-01 (04:[1][0-4]:[0-5][1-9]|04:15:00)' * | grep -ach '0110 478655 ..  51' | awk '{SUM += $1} END { print SUM }'`
ta=$((ta1 + ta2))
echo -e "Results =  $ta "

i wanted to add the results and put it on a while do statement, also i needed two grepping so i can sum up all the instances for a 15min cycle., any idea how to do a single grep statement instead?

share|improve this question
Your while loop doesn't make any sense to me. Why are you using read line? What is the input to this loop? You don't use the $line variable anywhere, so I can't figure out what the purpose of the loop is supposed to be. – Caleb Jul 2 '11 at 10:05
You're still struggling over the same issue‌​. As I've already told you, express your requirements more clearly. And when you write pipelines like this, look at what each step is doing, look at what awk is getting as input. – Gilles Jul 2 '11 at 10:18
I reviewed your other question (which I hadn't seen) and Gilles is right. You would get much better suggestions from us if you would stop trying to describe your problem using blocks of code and described in plain English what you expect each step of your scenario to look like and input and output formats. If you still can't get this together please edit your original question with the requested descriptions. – Caleb Jul 2 '11 at 10:36
up vote 1 down vote accepted

If your problem is just making grep match a 15 minute window with one regex instead of two, try breaking it down like this:


egrep '2011-07-01 (04:[0][0-9]:[0-5][1-9]|04:10:00)' 
egrep '2011-07-01 (04:[1][0-4]:[0-5][1-9]|04:15:00)'


egrep '2011-07-01 04:(0[0-9]|1[0-4]):[0-5][0-9]'

That should match the 15 minute block starting at 00:00 and ending 14:59. For the next three blocks use these:

# 15:00 to 29:59
egrep '2011-07-01 04:(1[5-9]|2[0-9]):[0-5][0-9]'

# 30:00 to 44:59
egrep '2011-07-01 04:(3[0-9]|4[0-4]):[0-5][0-9]'

# 45:00 to 59:59
egrep '2011-07-01 04:(4[5-9]|5[0-9]):[0-5][0-9]'
share|improve this answer
@user774187: I fixed my unmatched paren typo and added matches for the other four 15 minute blocks. – Caleb Jul 2 '11 at 10:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.