in reply to Strip escape sequences

Hi monk-beginner,

First off, I *think* what you mean for the string containing Escapes is:

my $line = "Stopping saslauthd: \x1b[60G[\x1b[0;31mFAILED\x1b[0;39m]\r +";

The character \x1b is an Escape character, which can alternatively be written as \e.

(It looks like a string you'd see as a result of stopping a service under Linux, and the escape characters it contains are there to display the text in a different, easy-to-read color.)

Next of all, are you just trying to eliminate the escape sequences from your string entirely?  For that, perhaps a regular expression will help; for example:

use strict; use warnings; my $line = "Stopping saslauthd: \x1b[60G[\x1b[0;31mFAILED\x1b[0;39m]\r +"; my $noesc = $line; $noesc =~ s/(\e\[\d+G|\e\[\d+(;\d+)*m|\r*)//g; print "noesc = '$noesc'\n";

The relevant line here is:

$noesc =~ s/(\e\[\d+G|\e\[\d+(;\d+)*m|\r*)//g;

which means:  Assign $noesc to the value of $line, and then remove all occurrences of "<Escape>[<DIGITS>G"  or   "<Escape>[<DIGITS>[;<DIGITS>...]m"  or   "\r" (carriage-return).

Regular expressions take some time to understand, but they are very powerful entities, and one of the great "power tools" of Perl.  The following documentation may be of help for learning more about regular expressions:


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Replies are listed 'Best First'.
Re^2: Strip escape sequences
by ikegami (Patriarch) on Dec 09, 2006 at 23:07 UTC
    (my $noesc = $line) =~ s/\e\[[^a-zA-Z]*[a-zA-Z]|\r//g;

    might work as a more general solution.

      Hi all Monks,

      I wasn't really clear in my previous message.

      I need to strip the escape sequences to clearly send the command output via mail to the postmaster.

      Thank you all for your hints.

      Now it works.

      Again thanks you all.

      Monk Beginner Phil