LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


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

Rep: Reputation: Disabled
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?
 
Old 11-28-2011, 07:51 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

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

Rep: Reputation: 67
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.
 
1 members found this post helpful.
Old 11-29-2011, 06:38 PM   #4
mariovrn
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, 06:40 PM   #5
mariovrn
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, 08:21 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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 08:22 AM.
 
  


Reply


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 10:12 AM
text match pipe to file then delete from original text file create new dir automatic tr1px Linux - Newbie 6 09-10-2008 10: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 03: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 10:50 AM
Redirecting stdout from tar to a text file. heatlill Programming 2 01-22-2002 11:27 AM


All times are GMT -5. The time now is 02:09 AM.

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