in reply to Expecting non-print chars from HP Procurve
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Expecting non-print chars from HP Procurve
by sgrey (Novice) on Nov 04, 2011 at 16:07 UTC | |
by roboticus (Chancellor) on Nov 04, 2011 at 22:17 UTC |