Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove ANSI codes when storing script output

Some programs makes beautiful progressbars and stuff using ANSI escape sequences. That's nice.

What's not nice though is that if i put the output of that kind of program into a file and then try to view it it's filled with strange escape sequences.

Is there a way to strip away all the ANSI codes while logging?

I usually log the output of a script this way:

./script >> /tmp/output.log
like image 880
Martin Avatar asked Jun 10 '11 13:06

Martin


People also ask

How do I remove ANSI?

You can use regexes to remove the ANSI escape sequences from a string in Python. Simply substitute the escape sequences with an empty string using re. sub(). The regex you can use for removing ANSI escape sequences is: '(\x9B|\x1B\[)[0-?]

Does PowerShell support ANSI codes?

PowerShell has many features that support the use of ANSI escape sequences to control the rendering of output in the terminal application that's hosting PowerShell. PowerShell 7.2 added a new automatic variable, $PSStyle , and changes to the PowerShell engine to support the output of ANSI-decorated text.

What is <UNK>x1B?

The code containing only 0 (being \x1B[0m ) will reset any style property of the font. Most of the time, you will print a code changing the style of your terminal, then print a certain string, and then, the reset code.


1 Answers

Try:

$ TERM=dumb ./script >> /tmp/output.log

If that doesn't work, it's because the ANSI codes have been hard-coded into the script, so there is no easy way to remove them. If it does, it's because it's doing the right thing, delegating things like pretty output to libncurses or similar, so that when you change the TERM variable, the library no longer sends those codes.

like image 79
Warren Young Avatar answered Oct 14 '22 00:10

Warren Young