monk-beginner has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

sorry but I'm a beginner and I've a problem maybe someone of you could easyly solve.

I capture the standard output of a command in one array (@res) and of course I have annoying escape sequences.

For example :
"Stopping saslauthd: \1b[60G[\1b[0;31mFAILED\1b[0;39m]\r"
I have this printale string using:
use String::Escape qw( printable unprintable ); $okstring = printable($line);
but really I was unable to strip all the escape sequences from the output.

I try, hardly, but all the code I write doesn't works.

Any ideas?

many Thanks

Monk-Beginer Phil

Replies are listed 'Best First'.
Re: Strip escape sequences
by liverpole (Monsignor) on Dec 09, 2006 at 22:24 UTC
    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$..$/
      (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
Re: Strip escape sequences
by jbert (Priest) on Dec 09, 2006 at 22:09 UTC
    What do you want to do with the string? If you print it to the terminal, the escape codes will have the effect they would have had anyway (colour, etc). This might be OK.

    If you want to process the output and find failure or success, the escape codes won't hurt that. You can still match the string against /FAIL/ or /OK/.

    And if you want to keep all wordchars and whitespace you could do something like:

    $str =~ s/[^\w\s]//;
    but it depends on what you are trying to achieve.