sgrey:

It looks like typical ANSI escape codes for controlling terminals (setting colors, coordinates, etc.). Generally they start with an ESC (0x1b) character and end with an upper-case letter[1]. So you could simply remove all character sequences starting with ESC until the first upper-case character, like so:

use strict; use warnings; # The ugly bit of text from your post my $text = <<EOTXT; <p>Simple code to connect and login to HP Procurve use(ing) Expect bel +ow, generates following output to STDOUT and PATH/FILE</p><p>^[[2J^[[ +?7l^[[3;23r^[[?6l^[[24;27H^[[?25h^[[24;27H^[[?6l^[[1;24r^[[?7l^[[2J^[ +[24;27H^[[1;24r^[[24;27H^[[24;1HConnecting to Tacacs server^[[?25h^[[24;1H^[[24;1H ^[[?25h^[[24;1H^[[?25h^[[24;1H^[[24;1HUsername: ^[[?25h^[[24;1H^[[?25h^[[24;11H^[[24;11H^[[?25h^[[24;11H^[[1;1H^[[?25l +^[[24;11H^[[24;1H ^[[?25h^[[24;11H</p> EOTXT # Remove all bits of text from an ESC character to the first uppercase + alphabetic $text =~ s/\x1b[^A-Z]+[A-Z]//g; print "Remaining text is '$text'\n";

When I run this, I get:

$ perl foobar.pl Remaining text is '<p>Simple code to connect and login to HP Procurve +use(ing) Expect below, generates following output to STDOUT and PATH/ +FILE</p><p>Connecting to Tacacs server Username: </p> '

Notes

[1]: The wikipedia article I linked to says the end character is from ASCII 64 to 126 (@ to ~), so I'm clearly wrong here. But since the code gives me the desired (i.e., what I expected to see) results, so I'll just note the discrepancy here, rather than update the code.

[2]: Since the code above is a cut-and-paste from my screen, the escapes are turned into the sequence '^['. So you may want to add the line:

    $text =~ s/^[/\x1b/g;

just after setting the value if you want to download the above code & execute it.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Expecting non-print chars from HP Procurve by roboticus
in thread Expecting non-print chars from HP Procurve by sgrey

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.