3

I have a live log file called log.log and want to catch some matching patterns and values in it:

Example: log.log is growing and we are searching for lines that have pattern "ResponseTime = VALUE" and we want to extract the matched VALUE:

I am executing:

tail -F log.log | grep ResponseTime | cut -d = -f 2 | tr -d " "

And I am expecting to see

VALUE1
VALUE2
.. etc

But it is not working ... what should I do?

Thank you, NaMo

===========

Thank you, it works now. I am using: inotail -f log.log | stdbuf -oL grep ResponseTime | stdbuf -oL cut -d '=' -f 2 | stdbuf -oL tr -d " "

NaMo
  • 31
  • 1
  • 3
  • What happens if you run `cat log.log | grep ResponseTime | cut -d = -f 2 | tr -d " "`? I suspect some data is being cached in the pipeline and it won't get flushed until more data is written to the `log.log` file (or EOF). – srgerg Mar 14 '11 at 06:35

4 Answers4

10

BASH FAQ entry #9: "What is buffering? Or, why does my command line produce no output: tail -f logfile | grep 'foo bar' | awk ..."

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Thank you, it works. I am using: inotail -f log.log | stdbuf -oL grep ResponseTime | stdbuf -oL cut -d '=' -f 2 | stdbuf -oL tr -d " " – NaMo Mar 14 '11 at 07:20
1

Try changing grep to stdbuf -oL grep.

See BASHFAQ/009 for details.

Mikel
  • 24,855
  • 8
  • 65
  • 66
1

The reason that it isn't working is that some commands don't flush STDOUT with each output. Therefore the later commands are never being passed anything.

I would use just one command after tail such as:

tail -F t.log  | sed '/ResponseTime/!d;s/ResponseTime\s+=\s+(\d+)/\\1/'
Lawrence Woodman
  • 1,424
  • 9
  • 13
-2

If you want to remove newlines from your output then you can any of the following:

| cut -d = -f 2|sed -e :a -e '$!N;s/\n//;ta' 

| cut -d = -f 2|tr -d '\n'

| cut -d = -f 2|awk '{printf "%s",$0}'
chx
  • 11,270
  • 7
  • 55
  • 129