Search code examples
linuxshellsedcuttail

Piping tail -f to cut to sed produces no output


I try to display a named pipe in a Terminal with this command:

tail -f textFile | cut -d " " -f 3- | sed -e "s/a/g&g/"

For some reason this produces no output.

It does work as expected if the -f is removed:

tail textFile | cut -d " " -f 3- | sed -e "s/a/g&g/"

or the cut statement is removed:

tail -f textFile | sed -e "s/a/g&g/"

or the sed statement is removed:

tail -f textFile | cut -d " " -f 3-

Only when all of these three things are there together, it suddenly doesn't produce any output anymore. The order of sed and cut don't make a difference. All of this makes it hard for me to blame the input or output buffering behaviour of any one or pair of these programms.

A possible solution to get the required functionality would be a while read line structure, but I would like to avoid initializing a command for every line, if at all possible.


Solution

  • I was experiencing something similar with a ping command that I wanted to filter.

    The following webpage seems to explain what the issue is (stdio buffering) http://www.pixelbeat.org/programming/stdio_buffering/

    The website points to a solution which involves disabling the buffering with the "stdbuf" command

    tail -f filename | stdbuf -o0 cut -d " " -f 3- | sed -e "s/a/g&g/"
    

    The above works well for me, and removing "stdbuf -o0" causes no output to be displayed.

    >stdbuf --help
    Usage: stdbuf OPTION... COMMAND
    Run COMMAND, with modified buffering operations for its standard streams.
    
    Mandatory arguments to long options are mandatory for short options too.
      -i, --input=MODE   adjust standard input stream buffering
      -o, --output=MODE  adjust standard output stream buffering
      -e, --error=MODE   adjust standard error stream buffering
          --help     display this help and exit
          --version  output version information and exit