GENWiki

Premier IT Outsourcing and Support Services within the UK

User Tools

Site Tools


computerfaq:strip_ansi_codes_from_a_text_file

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.

/data/webs/external/dokuwiki/data/pages/computerfaq/strip_ansi_codes_from_a_text_file.txt · Last modified: 2022/07/13 11:14 by genadmin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki