Luke Oliff.

TIL: Stripping ANSI Escape Codes From Terminal Output

·TIL·1 min read·Luke Oliff

Coloured terminal output is great for human eyes and terrible for log files. ANSI escape codes turn clean text into a mess of [32m and [0m sequences.

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

The sed pattern matches the ESC character followed by bracket, optional semicolons, and the letter m. That covers most colour, bold, and underline codes.

command_with_colours | sed -e 's/\x1b\[[0-9;]*[a-zA-Z]//g' > clean.log

The broader pattern catches cursor movement codes too. When piping CI output to files or Slack messages, strip the codes first and save everyone the noise.

Is there a dedicated tool?

Yes. strip-ansi is an npm package. For one off use, the sed one liner is faster than installing anything.