1

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):

G1tIG1syShtbMTsxSA0KG1s0OzRIICAgICAgICAgICAgICAgICAgICAgICAgICBDT05GSURFTlRJ
QUwgSU5GT1JNQVRJT04bW0sbWzU7NEgbW0sbWzY7NEgbW0sbWzc7NEhUaGlzIHdvcmsgY29udGFp
bnMgdmFsdWFibGUsIGNvbmZpZGVudGlhbCwgYW5kIHByb3ByaWV0YXJ5IGluZm9ybWF0aW9uLhtb
SxtbODs0SERpc2Nsb3N1cmUsIHVzZSwgb3IgcmVwcm9kdWN0aW9uIGlzIGdvdmVybmVkIGJ5IHlv
dXIgTGljZW5zZSBBZ3JlZW1lbnQuG1tLG1sxMDs0SBtbSxtbMTE7NEhUaGlzIHVucHVibGlzaGVk
IHdvcmsgaXMgcHJvdGVjdGVkIGJ5IHRoZSBsYXdzIG9mIHRoZSBVbml0ZWQgU3RhdGVzIGFuZBtb
SxtbMTI7NEhvdGhlciBDb3VudHJpZXMuICBUaGUgd29yayB3YXMgY3JlYXRlZCBpbiAxOTg4IGFu
ZCByZXZpc2VkIGluIDE5OTQuICAbW0sbWzEzOzRISWYgcHVibGljYXRpb24gb2NjdXJzLCB0aGUg
Zm9sbG93aW5nIG5vdGljZSBzaGFsbCBhcHBseTobW0sbWzE0OzRIG1tLG1sxNTs0SBtbSxtbMTY7
NEggICBDb3B5cmlnaHQgMTk4OCwxOTk0LiBBbGwgcmlnaHRzIHJlc2VydmVkLhtbSxtbMTc7NEgb
W0sbWzE4OzRIG1tLG1sxOTs0SFRoaXMgQ29weXJpZ2h0IG5vdGljZSBhbmQgb3RoZXIgY29weXJp
Z2h0IG5vdGljZXMgaW5jbHVkZWQgaW4gdGhlIG1hY2hpbmUbW0sbWzIwOzRIcmVhZGFibGUgY29w
aWVzIG11c3QgYmUgcmVwcm9kdWNlZCBvbiBhbGwgYXV0aG9yaXplZCBjb3BpZXMuG1tLG1syMTs0
SBtbSxtbMjI7NEhUaGlzIGlzIGEgcmVnaXN0ZXJlZCB0cmFkZW1hcmsuG1tLDQo=
  • 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
0

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.