I'm trying to see how many times foo bar
appears in /var/log/foo.log
within an arbitrary amount of time on a remote server, but nothing that I've tried so far has worked.
I've already got a timer script that I use to keep track of how long it has been since I started tailing /var/log/foo.log
, and now I'd just like a way to tell how many times foo bar
has appeared in the tailed output.
I searched google, but I didn't find anything pertinent within the first 10 pages of results.
Here's what I've tried with frustrating results:
## works on local machine, but doesn't work as expected on remote
tail -f /var/log/foo.log | grep foo\ bar | sed '='
## works on local, but not remote
tail -f /var/log/foo.log | grep foo\ bar | cat -n -
## works on local, but not remote
tail -f /var/log/foo.log | grep foo\ bar | awk -F'\n' '{printf "[%d]> ", NR; print $1}'
I even tried to write a sed script that'd act like tail -f
, but I made limited-to-no headway with that.
NOTE
the remote server is running an older version of coreutils, and upgrading is an option, but is NOT in any way the desired solution.
Best Answer
works for me and is the first what I thought of - that is if you really want the lines numbered from 1 and not with the real line number from the file watched. Optionally add
grep
if needed to the appropriate place (either before or afternl
). However, remember that buffering may occur. In my particular case,grep
has the--line-buffered
option, butnl
buffers it's output and doesn't have an option to switch that off. Hence thetail | nl | grep
combo doesn't really flow nicely.That said,
works for me as well. Numbering starts again from the beginning of the "tailing" rather than beginning of the whole log file.