What is Ansifilter? A Complete Guide to Cleaning ANSI Escape Codes

Written by

in

Any simple way to remove ALL escape sequences (except…

Any simple way to remove ALL escape sequences (except ) from a screen log? I am logging the SSH connection within a screen ses… Reddit·r/bash Removing colors from output – bash – Stack Overflow

There’s also a dedicated tool to handle ANSI escape sequences: ansifilter. Use the default –text output format to strip all ANSI … Stack Overflow

Remove ansi escape output from log file using cat / ansifilter …

Closed 1 year ago. Improve this question. I have this output from kubernetes conformance tests. Copy. test/e2e/common/ne… Stack Overflow

When managing log files from modern terminal applications, CI/CD pipelines, or Docker containers, you often end up with files cluttered by raw terminal formatting characters. These codes control colors, cursor movements, and line clears. To strip them down to plain text, administrators and developers primarily choose between two paradigms: a dedicated parser like Ansifilter or a stream editor like Sed.

Each approach comes with distinct architectural strategies and trade-offs. The Contenders: Architecture & Capabilities 1. Ansifilter

Ansifilter is a purpose-built, open-source utility designed to recognize, parse, and strip or convert ANSI terminal escape sequences.

How it works: It processes text through an internal state machine (similar to a real terminal emulator). Instead of using raw matching, it decodes the structure of Control Sequence Introducer (CSI) commands. Feature Highlights:

Strips colors, style formatting (bold/underline), and complex cursor commands smoothly.

Converts terminal data directly into other formats, such as HTML, LaTeX, RTF, or Pango markup.

Handles advanced 256-color palettes and TrueColor (24-bit RGB) sequences reliably. 2. Sed (Stream Editor)

Sed is a lightweight, ubiquitous POSIX utility meant for parsing and transforming text streams via Regular Expressions (Regex).

How it works: Sed does not understand terminal mechanics. It simply searches the data stream for bytes matching a specific character pattern and deletes or swaps them out. Feature Highlights:

Pre-installed on virtually every Linux, macOS, and Unix environment natively. Executes with near-zero overhead for simple lookups. Completely customizable via standard regex syntax. Key Comparison Metrics Feature / Metric Ansifilter Sed (Stream Editor) Parsing Mechanism Stateful terminal parser Regular expression string matching Reliability

Extremely High. Catches multi-byte and complex terminal commands. Moderate. Misses complex or malformed escape sequences. Availability

Requires manual package installation (apt install ansifilter). Omnipresent on Unix platforms by default. Output Flexibility Exports to Plain Text, HTML, RTF, LaTeX, and Pango. Strict plain text mutations only. Performance (Small Files) Fast, but involves external tool overhead. Microsecond execution via basic streaming logic. Performance (Massive Logs) Optimized for large data; parses sequentially.

Can choke or slow down with massive, multi-step regex lookups. Syntax and Implementation Examples How to use Ansifilter

By default, Ansifilter strips out codes and returns plain text. You can pipe text into it or process files in bulk:

# Clean a log file directly via standard flags ansifilter -i colored_server.log -o clean_server.log # Stream a live log and strip formatting on the fly tail -f live_app.log | ansifilter Use code with caution.

(Optional: Use -H instead of default parameters to convert your logs into a color-coded HTML webpage.) How to use Sed

Because Sed relies on regular expressions, you must supply a pattern that matches the ANSI structure. The typical ANSI sequence starts with an Escape byte ( or ), followed by a [ and various formatting numbers or letters.

# Strip standard Select Graphic Rendition (SGR) color codes sed -r “s/[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g” colored_server.log > cleanserver.log Use code with caution.

If you are dealing with sophisticated application logs (such as those containing window title shifts or cursor repositions), you need a much broader regex pattern to safely catch them:

# Advanced regex to drop broader CSI and control string sequences sed -E “s/([@-]|[[0-?][ -/][@-~])//g” colored_server.log > clean_server.log Use code with caution. The Verdict: When to Use Which? Choose Ansifilter if:

Data Accuracy is Vital: You want to avoid accidental regex data corruption or missed multi-byte sequences that can mess up automated log analytics engines like the Splunk Community platform.

You Want Pretty Formatting: You need to render terminal logs visually for stakeholders in dashboards via HTML or formatted documentation.

The Logs Use Complex Layouts: Your software uses interactive commands like backspaces, screen clearing (), or dynamic progress bars. Choose Sed if:

You Face Environment Constraints: You are troubleshooting a raw production box or minimal CI container where installing new packages like Ansifilter is blocked or impossible.

The Output is Basic: Your logging software only throws standard color flags ( for red, etc.) and avoids complex formatting elements.

You are Bundling Shell Scripts: You want your cleanup logic self-contained in a portable bash script without external software dependencies. Alternative Solutions worth Considering

If neither utility fully fits your system requirements, developers frequently leverage alternative patterns:

Fixing the Root Source (Best Practice): Most developer frameworks and tools allow flags to bypass formatting generation entirely, such as docker logs, npm –no-color, or mapping TERM=dumb directly into your execution environments.

Dedicated Text Processing Tools: Packages like ansi2txt or col -bp strip raw control paths gracefully for lighter server logs.

Viewer Flags: When reading raw files manually instead of writing them to storage, utilities like less -R read and render the colors properly without altering the underlying file data.

To best tailor this advice, could you share a bit more context?

What specific tools or languages (e.g., Docker, Jenkins, Node.js) are generating these log files?

Are you planning to clean these logs interactively on the command line, or within an automated software pipeline?

Any simple way to remove ALL escape sequences (except …

Any simple way to remove ALL escape sequences (except ) from a screen log? I am logging the SSH connection within a screen ses… Reddit·r/bash Removing colors from output – bash – Stack Overflow

There’s also a dedicated tool to handle ANSI escape sequences: ansifilter. Use the default –text output format to strip all ANSI … Stack Overflow

Remove ansi escape output from log file using cat / ansifilter …

Closed 1 year ago. Improve this question. I have this output from kubernetes conformance tests. Copy. test/e2e/common/ne… Stack Overflow

how do I remove all escape sequences from a file? : r/bash – Reddit

You can probably replace [fhlmpsuABCDEFGHJKST] with simply m if you are only changing colours. … I use these 2 methods very succ… Reddit·r/bash Remove ANSI codes when storing script output [duplicate]

Related * Redirect stdout to file without ANSI warnings. * how to print file containing text with ANSI escapes. * Finding ANSI esc… Stack Overflow

Removing control chars (including console codes / colours) from …

I needs the name of your scriptfile as command line argument, like ./name_of_script name_of_scriptlog . … script command capture… Unix & Linux Stack Exchange

Remove ANSI escape sequences from shell process output? : r/emacs

OP here, I’m looking for a way to accomplish this in Elisp. Thanks for your response though. I was looking at that thread earlier … Reddit·r/emacs

How to remove ^[, and all of the ANSI escape sequences in a file …

Comments. … You can use the astrp CLI tool for stripping ANSI escape codes. astrp is built on top of Alacritty’s VTE parser whic… Stack Overflow Removing ANSI characters from log – The FreeBSD Forums

That’s quite a difficult thing to do by software, because in order to remove them, they have to be interpreted – that is, the soft… Removing ANSI characters from log – The FreeBSD Forums

After being unable to find a solution to extracting ANSI escape sequences from logfiles, I thought I’d see if the problem was due … Process ANSI escape codes before piping – Super User

2 Answers. … should remove all escape codes that are not “Select Graphic Rendition” codes — things like color, bold, italics. Ed… Super User How to “un-ANSI-fy” logs? – Server Fault

* See: unix.stackexchange.com/questions/14684/… – unix.stackexchange.com/questions/111899/… stackoverflow.com/questions/17998978/…… Server Fault How to “un-ANSI-fy” logs? – Server Fault

About the only thing that seems up for debate is which regex is the best for which situation. Zoredache. – Zoredache. 2015-01-14 1… Server Fault Solved: Disable ANSI escape codes – Splunk Community

1) Use a terminal emulator which does not intepret (at least some particuar subset of) those ANSI codes. This option might be tric… Splunk Community Solved: Disable ANSI escape codes – Splunk Community

… interface running shell on a terminal that inteprets ANSI escape codes using a tool which does not sanitize the data before pa… Splunk Community Removing ANSI color codes from text stream – Super User

completely remove ANSI escape codes * CSI sequences: ESC [ followed by parameter bytes ( 0x30 – 0x3F ), optional intermediate byte… Super User Removing ANSI color codes from text stream – Super User

This sed script deletes every sequence while leaving ordinary text untouched. It handles escape codes that span line boundaries. I… Super User Everything you never wanted to know about ANSI escape codes

Who and where defined  and ? Those look like private controls. xterm defines and uses them. Looks like they we… Reddit·r/programming

Remove escape characters using sed – Unix & Linux Stack Exchange

Related * Using sed to get rid of characters < > , * Add variable to beginning of line, not using sed. * Remove all type of specia… Unix & Linux Stack Exchange strip_ansi_escapes – Axiom Docs

Use the strip_ansi_escapes function in APL to remove ANSI escape sequences from strings. ANSI escape codes are special character s… strip_ansi_escapes – Axiom Docs

Use the strip_ansi_escapes function in APL to remove ANSI escape sequences from strings. ANSI escape codes are special character s…

How to strip the ANSI escape codes from a text file? [duplicate]

* 2 Answers. Sorted by: As per the documentation of Notepad++ 7.8. Notepad++ regular expressions use the Boost regular expression … Stack Overflow

ansifilter: ANSI escape code processor and converter | Man Page

See the README file for details. * File options. -i, –input= (default stdin) Name of input file. -o, –output= (default stdout) N… ANSI escape code – Wikipedia

If n is 1 , clear from cursor to beginning of the screen. If n is 2 , clear entire screen (and moves cursor to upper left on DOS A… Removing ANSI escape sequences from a log file

Just another small note for self, from https://stackoverflow.com/questions/17998978/removing-colors-from-output. Frequently when r…

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *