0

I am runnung Kubuntu 20.04.

Command watch is from package procps.

dpkg -l procps

shows the version:

2:3.3.16-1ubuntu2

I am redirecting the output of warch to a file, like

watch -t "date" >/tmp/Wex1

Even if watch options -d and -g are not specified, and independent if option -c is specified or not, watch does write into the file ESC-sequences, presumably ANSI color sequences.

Somewhere in Internet I read that watch suppreses color sequences in output when not outputting to a terminal. But this is not true in my case.

Please do someone know how to prevent that watch outputs ESC-sequences?

  • addendum 28.04.2022:

thanks, but all is more complicated.

(a) I was able to discern which ESC sequences are for cursor movement and which are for color. That for cursor movement eI want to keep.

(b) The -c option of watch concerns if the command executed by watch does deliver colors to watch. This is not the case.

(c) The cammand "date" was only a shortened example for my post. The real is a chain of three commands: 1. df (within diff in order to compare with previous state), 2. free, 3. pmap. As follows:

warch -d "df -T -a >/tmp/WEx_df_a__2; diff -U0 /tmp/WEx_df_a__1 /tmp/WEx_df_a__2; echo \"\"; free -h -w; echo \"\"; pmap -X 2205860"

piped to less or not.

(d) Normally the nice output of watch I am observing by eyes. In special situations I want to make quasi a screenshot.

Another item: Because the terminals provided by Kubuntu by Konsole are /dev/pts/21 but not /dev/tty2, screendump and /dev/vcs are not usable.

Thus I have to write this output of watch to file for documentation and for parsing.

(e) What watch wrote to file I have processed to remove the color sequences.

I wrote a perl program using some of the regexes given in Internet in

https::/itectec.com/superuser/removing-ansi-color-codes-from-text-stream

(f) This my perl script grew and grew and is not finished. I have no way found to proceed because of many difficulties:

(f1) When putting the output of my perl script direct to the terminal (in Kubuntu, in Konsole, /dev/pts/21 e.g.) nothing is displaed. Only when piping the output to less, the output is displaed, with nonprintable characters in less its transformation.

(f2) By this need of using less, the layout as yielded from pmap is going lost, all words are a chain with BLANKS as delimiters.

I have also fed to

xxd -g1

but now forgotten.

Regards anton

  • addendum 30.04.2022:

Thanks. Now new realizations, all seen through, found many things to be done by the developers.

(A)

What I wrote is not fully correct: which ESC sequences are for cursor movement and which are for color.

The ESC sequences which are not for cursor movement are for highlighting. That is not done by color but by revers video.

For outputting in reverse video not ansi color codes are proper but other.

For my intention to parse the one-shot output of watch, I would have to substitute it with simple markers.

If it would be worth while considering other defects.

(B)

For the actual terminal in use, With

infocmp -L -1

one can see which control sequences are accepted by the terminal and with

xxd -g1

one can see which sequences are output by command watch.

There are some sequences outpt by watch that are not contained in my infocmp list for my terminal.

Command "watch" seems not to be designed for xterm-256-color.

(C)

When output of watch is not directly fed to terminal but is saved to file and file contemts fed to terminal, I experienced two failures:

(C1)

The setup of the terminal is destroyed.

When I type SHIFT-UPARROW the display is not moving to one line up (ss normal) but the capital letter "A" is displayed.

I have to type "reset" to restore.

I have found which output sequece of watch is the cause:

/e[?1049h\e[22;0;0t

This is the very beginning of watch output.

(C2)

The second failure is that nothing at all is displayed on screen from the contents of the file. Only by piping to less one can detect that an output exists.

This failure can be circumvent if the contents of the file is deleted from a certain position on.

I have found the position, the cause:

/e[?1049l\e[23;0;0t

Regards anton

  • addendum 01.05.2022

I am adding the following so you may be able to see all in context. The beginning of this story is in my post of 10.04.2022 .../questions/698648/...

In this my post I have forgotten to say what the PID 2205860 given to pmap command is. This is the PID of my main program running in another terminal, which is:

/media/.../wels_fullpath -d / 2>/dev/null | grep -v -e "^fd1" >/tmp/WEx_fullpath_of_slash_ie_mountpoint_of_cow_fs_overlay__unsorted

This my main program is running since many days in order to help me analyze who had done what so that overnight in the overlay fs very many GB were stolen.

Before

df -Th -a had shown

/cow overlay 16G 1.3G 15G 8% /

My initial question, where the buffers of "less" are, I have circumvented and do not use "less" for this more.

Regards antondhdih

2
  • 1
    Are you sure they are color codes (have you looked at the contents of /tmp/Wex1 using less -R for example)? I think perhaps what you are seeing are code sequences for positioning the terminal's cursor Apr 23 at 20:55
  • ... as well, the -c option is about the interpretation of color codes in the output of the command being watched; compare for example watch 'echo foobar | grep --color=always oo' versus watch -c 'echo foobar | grep --color=always oo' Apr 23 at 21:03

2 Answers 2

0

EDIT: I also can't get watch to redirect output without some sort of control sequences into a file. See Output of 'watch' command as a list for workarounds.

Just for ruling out any weird things happening because of your setup, could you try and compare the outputs of:

# ESC sequences should be visible. less -R should not output colors ans the output is broken.
watch -t "/bin/ls / --color=always" >/tmp/watch_nocolor.log

# different ESC sequences expected. less -R should show colors but again, broken output.
watch -ct "/bin/ls / --color=always" >/tmp/watch_colorinterpret.log

ls on Kubuntu should behave the same as on my instance of Lubuntu (where I can reproduce your problem).

Maybe try piping your watched command through cat, watch -t 'date | cat' > /tmp/Wex1. It should not behave differently, though. However, put somewhere in-between your whole command could make a difference because most programs should notice they are not going to output onto a terminal.

0

watch is meant to let you see on screen the differences between different runs of the command. In order to position the output from the next execution it has to move the cursor (using ANSI escape sequences).

No way to remove that, that I know of. Basically the program is not meant to do what you expect.

An alternative is just a shell loop, so instead of:

$ watch -t date >/tmp/Wex1

you can do something along these lines in bash (2 seconds is watch default between executions):

$ while date; do sleep 2; done >/tmp/Wex1

Check the loop constructions for your shell, because a non-zero exit code from the program could stop the loop, if that's what you want, depending on where you place it.

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.