0

For example, I run the top command and store it to a file in Linux, after that I open that file in Windows it contains some gibberish. Here is the file viewed in Notepad++:

File contents

The option to convert to UTF-8 in Notepad++ doesn't work.

How can I read that file in Windows? I tried using dos2unix but it doesn't work and gives the error:

dos2unix: Binary symbol 0x1B found at line 1

Edit: Trying the sed 's/\x1b\[[0-9;]*[a-zA-Z]//g' file.txt command gives the expected output but only in the terminal

Destroy666
  • 5,299
  • 7
  • 16
  • 35
TeaViris
  • 11
  • 2
  • 1
    The right way is to tell tools like `top` not to print these characters in the first place. Some tools modify their behavior automatically if their stdout is not a terminal. It seems `top` does not do this. Use `-b`, e.g. `top -b -n 1 >output`; see `man 1 top`. In general, if some `troublesome_program` provides no option to turn ANSI escape sequences off, you can try passing `TERM=dumb` in the environment, e.g. `TERM=dumb troublesome_program`. – Kamil Maciorowski Apr 19 '23 at 09:26

1 Answers1

2

These are ANSI escape sequences/codes, so basically ESC character followed by some other characters that are used as sequences to display colors, etc.

To remove them, you can for example use ansi2txt CLI tool, e.g.:

top | ansi2txt > output.txt

Here are also more ways to remove them: Removing ANSI color codes from text stream

Destroy666
  • 5,299
  • 7
  • 16
  • 35