Why no output is shown when using grep twice?

后端 未结 5 854
心在旅途
心在旅途 2020-12-02 16:46

Basically I\'m wondering why this doesn\'t output anything:

tail --follow=name file.txt | grep something | grep something_else 

You can ass

5条回答
  •  难免孤独
    2020-12-02 17:21

    Figured out what was going on here. It turns out that the command is working it's just that the output takes a long time to reach the console (approx 120 seconds in my case). This is because the buffer on the standard out is not written each line but rather each block. So instead of getting every line from the file as it was being written I would get a giant block every 2 minutes or so.

    It should be noted that this works correctly:

    tail file.txt | grep something | grep something
    

    It is the following of the file with --follow=name that is problematic.

    For my purposes I found a way around it, what I was intending to do was capture the output of the first grep to a file, so the command would be:

    tail --follow=name file.txt | grep something > output.txt
    

    A way around this is to use the script command like so:

    script -c 'tail --follow=name file.txt | grep something' output.txt
    

    Script captures the output of the command and writes it to file, thus avoiding the second pipe.

    This has effectively worked around the issue for me, and I have explained why the command wasn't working as I expected, problem solved.

    FYI, These other stackoverflow questions are related:
    Trick an application into thinking its stdin is interactive, not a pipe
    Force another program's standard output to be unbuffered using Python

提交回复
热议问题