Frequently Asked Question

Strip Ansi Codes From A Text File
Last Updated a year ago

ANSI or not to ANSI

ANSI codes in the terminal allow simply color attributes to be applied to things like log files. Asterisk is a good example which highlights various log file data with colours, but when you're opening this in a text editor that's not ansi aware (most of them) then you'll see a bunch of ^[ and other code.

Stripping this is fairly easy and we'll do it with sed…

cat yourlogfile.log |sed -e 's/\x1b\[[0-9;]*m//g' > newlogfile.log

Here we're taking the encoded logfile, passing it through sed which will strip out the ANSI codes, and then putting it back in a new log file.

Effectively we're looking for ^[ followed by digits to ]*m and replacing that with nothing.

Powered by GEN UK CLEAN GREEN ENERGY

Loading ...