I have a text file that contains [ANSI ?] escape sequences:

raw file

When I cat the file I get formatted output:

formatted output

How do I save / pipe the output of the text file to a new file so that the control codes are removed but the formatting is preserved ?

In other words, I want to convert / export the contents of the file to a new file while retaining the intended display format (eg. like one would get when using less -r <file>).

I need an automated way to do this so that the output can be logged and sent as an email notification.

Here is the base64 output of the file in the above screenshots (use base64 -d to decoded it):

  • 1
    How can "pure" text have formatting? – muru Jan 13 at 4:31
  • cat it in a terminal emulator, select the text, copy it, and then paste it into another file. Alternatively, you can write a script which interprets just the escapes present in the file (clear, cup, el), but it's not worth the trouble (just like doing research & evaluation for some already existing program doing that). – user414777 Jan 13 at 6:13
  • e.g. in xterm you press Shift-PageUp to scroll back until the beginning of the text, Left-Click at the start of selection, scroll back down with Shift-PageDown, Right-Click to the end of selection, then enter cat > outfile and press Shift-Insert and Control-D to save it into another file. – user414777 Jan 13 at 6:19
  • How do I save / pipe the output of the text file to a new file so that the control codes are removed but the formatting is preserved? - it's possibly only if by formatting you mean indentation, paragraphs, whitespaces etc. – Arkadiusz Drabczyk Jan 13 at 21:49
  • With xterm, you can dump the screen to html or svg. Using screen or tmux, you can dump the screen to text in a different way. But "formatting" is meaningless, since there are many different ways to represent the screen contents. – Thomas Dickey Jan 21 at 1:47

Easy way it to use truncate command with delete option. \033 represent escape character.

$cat your_filename|tr -d \033 > save_filename

use following command to remove all the control characters

$cat your_filename|tr -d [:cntrl:] > save_filename

Hope that helps

New contributor
Niranjan Shrestha is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

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.