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$..$/

In reply to Re: Strip escape sequences by liverpole
in thread Strip escape sequences by monk-beginner

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.