0

In this question, How to 'grep' a continuous stream? , we can have continuous grep on a particular file.

But how about counting the results of grep.

I have tried something like this, but no luck

tail -f logs/log* | grep --line-buffered -c "pattern in here"

EDIT:

Okay, let's say I have a log file that continuously append the word "success" to a file if some operation was completed.

Therefore, the log file would look something like this:

{date} ==> error
{date} ==> error
{date} ==> error
{date} ==> success
{date} ==> error
{date} ==> success

It is continuous, now I want to continuously tail the count of the "success" in all logs

e.g.

log1 ===> 3
log2 ===> 4
log3 ===> 2

Is that possible?

Community
  • 1
  • 1
Joey Hipolito
  • 3,108
  • 11
  • 44
  • 83
  • so what do you want the output to look like? just the count increasing from 2 to 3 to 4 to .... ? (I dont' think that is possible). Please edit in your question you required results. Good luck. – shellter Apr 10 '14 at 16:15
  • How do you expected grep to tell you how many patterns it has seen in a file before it has finished seeing all the file? – Mark Setchell Apr 10 '14 at 16:16

1 Answers1

0

Try this:

#!/bin/bash
set -m

# Set up control-C trap to kill all children
trap '{ echo "Interrupted." ; pkill -P $$; exit 1; }' SIGINT

for f in logs/log*
do
   tail -f "$f" | awk -v f="$f" '/success/{printf "%s: %s - %d\n",f,$0,++i}' &
done
wait   # So we can be interrupted and then kill our children
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • I have updated this so you can see names of all logs being tailed and have a running count of matches for each. – Mark Setchell Apr 11 '14 at 11:39
  • 1
    Did this work out for you? If so, can you consider accepting my answer please so I get a lovely big green tick. If not, please say what didn't work, so I, or someone else, can help you further. Thank you. – Mark Setchell Apr 22 '14 at 21:13