SED IO Redirection – Cannot Redirect Output

io-redirectionsed

I'm piping output from clock through sed to remove leading zeroes from numbers.
It looks like this:

clock -sf 'S%A, %B %d. %I:%M %P' | sed 's/\b0\+\([0-9]\+\)/\1/g'

That works fine and produces the output I want.

However, when I try to redirect the output to a file, nothing is written to the file. The following does NOT work.

clock -sf 'S%A, %B %d. %I:%M %P' | sed 's/\b0\+\([0-9]\+\)/\1/g' > testfile

Nothing is written to testfile. What am I doing wrong?

Best Answer

You're running into an output buffering problem. sed normally buffers its output when not writing to a terminal, so nothing gets written to the file until the buffer fills up (probably every 4K bytes).

Use the -u option to sed to unbuffer output.

clock -sf 'S%A, %B %d. %I:%M %P' | sed -u 's/\b0\+\([0-9]\+\)/\1/g' > testfile
Related Question