There is another question which is nearly the same, but none of the suggestions from its answer works for me: Piping tail -f into awk

I'm trying to tail -f a logfile but want to replace \r with \n before displaying it, so that the terminal doesn't overwrite itself but instead allows me to scroll through the output.

this works as suspected:

tail -f -c 1000 "mycustom.log"

and this also:

tail -c 1000 "mycustom.log" | mawk 'gsub("\r","\n")'

none of those three however ever print anything to my console:

tail -f -c 1000 "mycustom.log" | mawk 'gsub("\r","\n")'
stdbuf -o0 tail -f -c 1000 "mycustom.log" | mawk 'gsub("\r","\n")'
unbuffer tail -f -c 1000 "mycustom.log" | mawk 'gsub("\r","\n")'

For unbuffer to be known to my system I had to install the package expect-dev

using awk or mawk doesn't seem to make a difference. I've also written a sed variant:

sed 's/\r/\n/g'

which behaves the same for me.

I've also found this question:
But the sed -u parameter doesn't change anything for me, and the $'..' construct is identified as syntax error by my sed - changing it to \$'...' does run but with tail -f piping to it doesn't ever print anything just like everything else I tried.

Is there anything else I could try? Is the problem that I use a global command in sed / awk? Or maybe is the problem that the original stream produced by tail -f is only "one line" since there are no \n in it? Is there some way to make sed or awk process smaller chunks than line by line?

Is there an alternative to replace all \r with \n that might work with tail -f?

  • What is the line terminator in your custom log? Normally lines in Linux text files (including logs) end with \n and have no \r. While Window-based text files normally end with \r followed by \n. Just to have \r as line terminator is strange I think. – gogoud Mar 4 '15 at 12:09
  • yes its strange, but the result of ffmpeg status which seems to do so on purpose to always overwrite the last line – kaefert Mar 4 '15 at 12:12
  • Have you tried | tr "\r" "\n"? – G-Man Says 'Reinstate Monica' Mar 4 '15 at 18:05
  • nope, I did not try that, and yes it works! thanks G-Man! (if you wan't post it as answer and I'll accept it) – kaefert Mar 4 '15 at 22:58

Okey, so I've found my answer: since my logfile looks like a single line to awk and sed that is never completed (in case of tail -f) they won't ever output anything. So I've written this workaround:

tail -c 1 -f "$path" | while read -r -n1 char
  if [ "$char" = $'\r' ] ; then
    echo ""
    echo -n "$char"

this is ugly, but it works ;)

UPDATE: User G-Man has suggested a much nicer way that works as well:

tail -c 1 -f "$path" | tr "\r" "\n"

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.