How to Strip ANSI Colour Codes and Escape Sequences from Text in Linux Terminal

Modern Linux command-line utilities, compilers, test runners, and package managers often format their terminal outputs with ANSI escape sequences. These control characters add colourful text, bold styling, and terminal highlights. However, when you redirect this output into log files, parse it with text processing pipelines, or send it to external alerting services, raw ANSI codes appear as unsightly gibberish (such as ^[[0;31m or \x1b[32m). Stripping these escape sequences is essential for clean text archiving and automated processing.

You can strip ANSI colour codes and formatting characters in the Linux terminal using standard stream editors like sed, dedicated utility packages like ansifilter, or standard perl one-liners.

Stripping ANSI Codes Using sed

Because sed (stream editor) is installed by default on virtually all Linux distributions, it is the most accessible tool for removing escape sequences on the fly.

To filter text from a piped command:

command | sed -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,2};?)?)?[mGK]//g"

To strip ANSI codes from an existing log file and write the cleaned result to a new file:

sed -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,2};?)?)?[mGK]//g" build.log > build_clean.log

How the regular expression functions:

  • \x1B\[: Matches the ASCII escape character (hex 1B or octal 033) followed by the opening bracket [, known as the Control Sequence Introducer (CSI).
  • ([0-9]{1,3}(;[0-9]{1,2};?)?)?: Matches optional numeric styling parameters separated by semicolons (such as 0;32 for green text).
  • [mGK]: Matches standard terminal control terminators: m (Select Graphic Rendition for colours and weights), G (horizontal cursor positioning), and K (line clearing).
  • //g: Replaces all matching sequences across each line with an empty string.

Using ansifilter for Comprehensive Terminal Output Cleaning

For complex terminal recordings, progress bars, or escape sequences that include cursor movements, the dedicated ansifilter package provides a robust and reliable solution.

  1. Install ansifilter via your package manager:
    • On Ubuntu / Debian: sudo apt install ansifilter
    • On Fedora / RHEL: sudo dnf install ansifilter
    • On Arch Linux: sudo pacman -S ansifilter
  2. Pipe output directly into ansifilter:
    pytest --color=yes | ansifilter > test_results.txt
  3. To clean an existing file in-place or generate clean text output:
    ansifilter -i terminal.log -o clean.log

Stripping Escape Sequences Using a Perl One-Liner

If you work across diverse POSIX environments where sed dialect differences (such as BSD sed on macOS vs GNU sed on Linux) cause regular expression compatibility issues, a perl one-liner offers predictable behaviour:

cat terminal_output.txt | perl -pe 's/\e\[?.*?[\@-~]//g' > plain_output.txt

This command matches the escape character \e and cleanly eliminates any sequence ending with an ASCII character in the command range @-~.

For more advanced bash scripting and text inspection workflows, consult our guide on how to check and debug bash scripts in Linux terminal using ShellCheck.

Troubleshooting Unstripped Characters and Binary Artifacts

If strange characters remain after running a filter, consider these troubleshooting steps:

  • Carriage returns (\r): Command-line progress bars frequently overwrite lines using carriage returns rather than ANSI sequences. Pipe through tr -d '\r' or col -b to remove residual backspaces and returns.
  • Application-level disable flags: Whenever possible, configure the originating tool not to emit colour codes in non-interactive sessions. Most utilities support flags like --no-color, --color=never, or honour the standard environment variable NO_COLOR=1.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.