LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Redirecting terminal output to text file leads to corrupted text file (https://www.linuxquestions.org/questions/linux-newbie-8/redirecting-terminal-output-to-text-file-leads-to-corrupted-text-file-916079/)

mariovrn 11-28-2011 06:04 PM

Redirecting terminal output to text file leads to corrupted text file
 
Hi!

Im trying to save the values of the temperature sensors on a text file to plot them afterwards by saving them into a text file, but the format of the output file is kindof corrupted. This is what I do:

$ watch -n 5 sensors >> temperatures.txt

And the text file I get looks like this:

[?1049h(B[?7hEvery 1,0s: sensorsMon Nov 28 22:59:37 2011coretemp-isa-0000
Adapter: ISA adapter
Core 0:+48.0C (high = +90.0C, crit = +90.0C)
coretemp-isa-0001
Adapter: ISA adapter
Core 1:+47.0C (high = +90.0C, crit = +90.0C)8940150512494834529549868778

The weird symbols are not something wrong with the post, that's exactly what I get. I was expecting that it would save what I see on the terminal, on a clean way one after the other and of course readable. Any clue of what's going on?

grail 11-28-2011 07:51 PM

Do you get the same if you do not redirect to the file? Looks like an encoding issue to me.

jthill 11-28-2011 08:27 PM

You don't need or want watch for that, just
Code:

while true; do sensors; sleep 5; done >> temperatures.txt
What you're seeing in your file are terminal control sequences; if you cat that file to your terminal you'll see watch's fullscreen output replayed.

mariovrn 11-29-2011 06:38 PM

Thanks a lot, jthill, that worked just fine! What I dont really understand is that when I do it with only once (not using watch, just redirecting the output from sensors to a file once) it does it fine, but when I use watch so that it writes several outputs is when the weird stuff appears...

mariovrn 11-29-2011 06:40 PM

@ grail, when i tell it to show it on the terminal (not redirecting the output to a file) it does it fine...

David the H. 11-30-2011 08:21 AM

As jthill explained, these are ansi escape codes, which control how the terminal displays information, in a way somewhat similar to html tags. When inserted in a text stream they aren't displayed directly; instead they tell the terminal how to color and position the text on the screen.

But they're still just ascii characters like all the rest. When this text is viewed in a normal text reader, they become visible, since the reader doesn't know how to interpret them. Again, it's like when you view a raw html file.

sensors apparently doesn't use escape codes in its output, and so when you run it stand-alone all you get is the plain text output. In fact the majority of programs, even when they do use escapes for formatting, are designed to not send them through pipes and redirects by default. That's why your single run and the looping solution works.

If I'm understanding it right, according to the watch man page, by default it strips out any escape codes from the output of the command it's monitoring. But it obviously must also insert its own codes into the output, since it wouldn't be able to control and update the display every few seconds without them. So when you pipe this output to a file, you're getting the escapes inserted from watch along with the raw data from sensors.


All times are GMT -5. The time now is 10:36 AM.