Home > Software engineering >  How to make "watch" command process \r (carriage return) correctly?
How to make "watch" command process \r (carriage return) correctly?

Time:08-29

I am trying to use watch command to watch the content changes in a file.

However, the file contains a progress bar, which uses the \r character to overwrite on itself. The watch command seem to ignore these \r characters.

For example, if I create a file that contains a carriage return:

$ echo -e "Looooong-string \r short-string" > test.txt

If I cat the file, the contents overwrites on itself correctly:

$ cat test.txt 
 short-stringng

But if I watch this cat command, the \r will be ignored.

$ watch cat file.txt
Every 2.0s: cat test.txt                    Sun Aug 28 21:39:59 2022

Looooong-string   short-string

Is there any way to make watch process these \rs correctly and make the string overwrite on itself?

CodePudding user response:

From the manual I can read

Non-printing characters are stripped from program output.  Use cat -v as part of the command pipeline if you want to see them.

And if you ran watch cat -v test.txt the output is

Looooong-string ^M short-string

CodePudding user response:

Doing

while :
do
    cat test.txt &
    sleep 2
    clear
done

works reasonably well.

  • Related