Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 11-28-2011, 05:04 PM   #1
LQ Newbie
Registered: Nov 2011
Posts: 3

Rep: Reputation: Disabled
Redirecting terminal output to text file leads to corrupted text file


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)
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?
Old 11-28-2011, 06:51 PM   #2
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
Do you get the same if you do not redirect to the file? Looks like an encoding issue to me.
Old 11-28-2011, 07:27 PM   #3
Registered: Mar 2010
Distribution: Arch
Posts: 211

Rep: Reputation: 67
You don't need or want watch for that, just
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.
1 members found this post helpful.
Old 11-29-2011, 05:38 PM   #4
LQ Newbie
Registered: Nov 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
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...
Old 11-29-2011, 05:40 PM   #5
LQ Newbie
Registered: Nov 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
@ grail, when i tell it to show it on the terminal (not redirecting the output to a file) it does it fine...
Old 11-30-2011, 07:21 AM   #6
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,842

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
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.

Last edited by David the H.; 11-30-2011 at 07:22 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Redirecting text from a file into a command simplified Linux - Newbie 5 07-13-2009 09:12 AM
text match pipe to file then delete from original text file create new dir automatic tr1px Linux - Newbie 6 09-10-2008 09:40 PM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 02:36 PM
in Pascal: how to exec a program, discard text output or send to text file Valkyrie_of_valhalla Programming 6 05-02-2007 09:50 AM
Redirecting stdout from tar to a text file. heatlill Programming 2 01-22-2002 10:27 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:07 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration