Ano-Tech Computers
Enter keyword:

Perl: Remove ANSI sequences from a string
Problem:
When processing terminal data or text files containing ANSI escape sequences, it can sometimes be useful to filter these out
 
Solution:
sub remove_ansi {
for (@_) {
$_ =~ s/\033\[[\?|0-9|\;]+?[a-z]//gi;
}
}

Use like 'chop' and 'chomp', for example:

remove_ansi $string; # Remove ANSI from $string
remove_ansi $a, $b; # Remove ANSI from $a and $b

Note:
$string = remove_ansi $string; # Does NOT do what you expect!

 
Discuss this solution
Did this article solve your problem? Yes No Did not apply

We welcome anyone who is willing to contribute to this public knowledge base, contact siteadmin@atc.no if you have information you would like to share. The idea is not to replace the commercial support sites, but to publish those hard-to-find solutions you've found yourself looking for over and over again.

Show all articles