3

When performing a grep like:

$ tail -f apilog_2014.07.09.log | grep "HELLO" | grep "99999"

I get the desired output:

12:22:35 server apache2:  HELLO FRIEND 99999
12:22:35 server apache2:  HELLO FRIEND 99999
12:22:35 server apache2:  HELLO FRIEND 99999

However if I do:

$ tail -f apilog_2014.07.09.log | grep "HELLO FRIEND" | grep "99999"

I don't get anything.

My locale settings are all set to en_US.utf8 and I've tested with [[:space:]] and with \s. No results. The file itself is text/plain; charset=us-ascii (checked with file -bi apilog_2014.07.09.log).

The files themselves are written with rsyslog, if that is any type of hint. Using Ubuntu 12.04.4 LTS.

Is there anything I'm missing?

As requested:

$ grep "HELLO" apilog_2014.07.09.log | od -c
0000000   1   4   :   2   7   :   0   0       s   o   f   i   a       c
0000020   a   r   l   o   s   :           A   P   I   L   O   G       H
0000040   E   L   L   O       F   R   I   E   N   D       t   e   s   t
0000060   i   n   g  \n   1   4   :   3   1   :   4   5       s   o   f
0000100   i   a       c   a   r   l   o   s   :           A   P   I   L
0000120   O   G       H   E   L   L   O       F   R   I   E   N   D
0000140   t   e   s   t   i   n   g       6   3   9   0   3  \n
0000156

UPDATE 14 Jul 2014

Answer here: Grep with spaces suddenly doesn't work

Related question (same solution): Piping from grep to awk not working

Carlos D
  • 41
  • 4
  • 1
    There is probably a strange character between `HELLO` and `FRIEND`. Please run `grep HELLO apilog_2014.07.09.log | od -c` and post the output here. – terdon Jul 09 '14 at 10:56
  • 1
    Please [edit] your question to add extra information, it is hard to read and easy to miss in the comments. Also, please post the output of the exact command I asked for. – terdon Jul 09 '14 at 12:34
  • Hey trendon, I've posed this on a gist for easier reading, running the command as you typed it https://gist.github.com/charlydagos/de3470c65dd99d04cbc0 – Carlos D Jul 09 '14 at 12:39
  • 2
    I would try: `grep -e 'HELLO.*FRIEND' tst.txt`. – slm Jul 09 '14 at 12:41
  • 2
    Ah, sorry, you must have `grep` aliased to `grep --color=always`. That's not a very good idea, you might want to change that to `grep --color=auto` (see [here](http://unix.stackexchange.com/q/138923/22222)). Anyway, could you please post the output of `\grep HELLO apilog_2014.07.09.log | od -c` instead? That way the color codes of the matched portion won't confuse things (the `\ ` will bypass any aliases and run the command directly). – terdon Jul 09 '14 at 13:00
  • Does this work? `grep -E "HELLO[^\S+]FRIEND" file` – user13107 Jul 09 '14 at 13:06
  • Hey terdon, here's the gist with two outputs, one like you said and, since you're right I do have grep aliased to `--color=always` (changed that now to `--color=auto`), I tried with with `auto` again. Hopefully it yields some help :) https://gist.github.com/charlydagos/ae38cbe30577225188c5 Thanks guys. – Carlos D Jul 09 '14 at 13:58

1 Answers1

1

Found the issue. If you recall at the beginning of the question, I'm doing primarily tail -f. I was mistaken when I said that there was no output, I left a console running and eventually (10 minutes later), there was output. It's explained with further detail here

http://www.ateamsystems.com/tech-blog/grep-output-is-delayed-or-no-output-when-piping-or-using-multiple-grep-calls-with-pipes/

Why the space character affects the time between outputs is still a mystery, but following the issue in that link solved it.

Basically I edited my .bashrc

alias grep="grep --color=auto --line-buffered"

And now I get the desired output.

Carlos D
  • 41
  • 4