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?

  • I don't have the clock command on my Linux system, what does it do. Does it produce one line of output and exit, or does it produce continuous output? – Barmar Sep 1 '14 at 20:29
  • With these flags it produces continuous output. – Semicolon Sep 1 '14 at 20:45
  • Running the second command without the sed pipe (redirecting clock directly into the file) works as expected. Running the second command without -s results in a single line being written to the file, as expected. – Semicolon Sep 1 '14 at 20:52

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
  • 3
    -u is a GNU specific option – mikeserv Sep 1 '14 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.