8

I executed the following command

# top > /home/user/top_output.txt

Nothing Happened for a while and then i pressed Ctrl+C. When i checked the file that was created, it had the contents in it. So i fired cat command on it and it gave me this output.
cat output of the text file


But when i tried the same thing with the less command i got this.

less output of the files


According to this post the job of Cat,less or More is just to display the contents of the file not translating the encoding. Can someone please tell me what's happening here?
P.S: I'm currently using Fedora 19

14

The escape sequences ESC [ ... m are called ANSI Escape Sequences. top sends them to your terminal to make it format output in color, bold, inverted text and so on. You never see these characters when running top but you see the resulting format. You could think of it as looking at a webpage in a browser - you don't see the <html>... formatting the content.

When dumping the output of top into a file, you are saving the non-printable escape sequences with everything else. Think of it as saving view source in your browser.

The default for less is to escape terminal control characters, displaying them in a printable form.

The default for cat is to pass them through to your terminal which interprets them and makes it look "normal".

Try less -r /home/user/top_output.txt

   $ man less ...
   -r or --raw-control-chars
          Causes "raw" control characters to be displayed.  
           The default is to display control characters using the caret
           notation; for example, a control-A (octal 001) is displayed as 
           "^A". Warning: when the -r option is used, less cannot keep 
           track of the actual appearance of the screen (since this depends
           on how the screen responds to each type of control
           character).  Thus, various display problems may result, such
           as long lines being split in the wrong place.

Compare to cat -v /home/user/top_output.txt which will escape non-printable characters.

5
  • the cat -v command gave me control characters as in ^[(B and ^[m in the output which was pretty garbled – Aditya Cherla Feb 9 '14 at 11:50
  • 1
    thanks! less -r solved it.now its displaying in proper format. But i did'nt understand why that happened, as you see there are no special characters except % : and commas – Aditya Cherla Feb 9 '14 at 11:51
  • 1
    When you run top in the terminal, you will see colors and other formatting (inverted text, bold etc). All these formats are caused by non-printable control-characters. So in your file there are lots of non-printable characters. % : , are printable and have nothing to do with it. – grebneke Feb 9 '14 at 11:54
  • cat -v: yes exactly, -v makes cat show you all the non-printable characters by escaping them. It is definitely garbled. This garble is what your terminal normally sees and interprets into color/formatting before displaying it to you. – grebneke Feb 9 '14 at 11:56
  • When I try less -r on an export of the Windows 10 registry, I still get different looks compared to when using cat. The output from cat is human readable, while the output from less -r is all black and seemingly sporadically prints <FF><FE>W when I press the up and down arrows. – HelloGoodbye Aug 7 '18 at 10:28

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.