[Tips] How to remove control characters from terminal recording (i.e. by script command)

Share your HowTo, Documentation, Tips and Tricks. Not for support questions!.
Post Reply
Message
Author
Aki
Global Moderator
Global Moderator
Posts: 3954
Joined: 2014-07-20 18:12
Location: Europe
Has thanked: 109 times
Been thanked: 520 times

[Tips] How to remove control characters from terminal recording (i.e. by script command)

#1 Post by Aki »

Hello,

Just a tip on how to remove control characters from terminal logs (i.e. generated with the script [1] command).

The ansi2txt program is required (from the colorized-logs package [2]).

You can use the following command [3] where log.txt is the log generated by the script command (or similar commands):

Code: Select all

cat log.txt | ansi2txt | col -b > log-cleared.txt
Hope this helps.

--
[1] SCRIPT(1)
[2] colorized-logs
[3] Removing control chars (including console codes / colours) from script output
⢀⣴⠾⠻⢶⣦⠀
⣾⠁⢠⠒⠀⣿⡁ Debian - The universal operating system
⢿⡄⠘⠷⠚⠋⠀ https://www.debian.org
⠈⠳⣄⠀

User avatar
CloisteredNeuron
Posts: 130
Joined: 2024-05-25 04:02
Has thanked: 68 times
Been thanked: 5 times

Re: [Tips] How to remove control characters from terminal recording (i.e. by script command)

#2 Post by CloisteredNeuron »

Thanks Aki :D

I was trying to save the GNOME Terminal buffer and a user suggested the script command. It works great other than the control characters. I created an awk script:

Code: Select all

clout.sh #Clean script output
#Create output with - script -a o1 --timing=t1
#Run with sh clout.sh o1 | less
#First search element should be ^M (hold ctrl and press V and then M)
awk '{gsub(/\033\[01\;36m|\033\[01\;32m|\033\[01\;34m|\033\[0m|\033\[01\;34m|\033\[\?2004l|\033\[\?2004h/,"")}1' $1
that removed control sequences from a single test case but ansi2txt is SO much better.

Thanks for sharing this!

Post Reply