Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eliminate non-visible characters perl

I have a perl variable that is being read in from another script's STDOUT:

$var = `someScript.sh`
print $var    <---- Prints "somestring"

However, the variable contains more than "somestring". There are 15 more characters on the front of the variable (special and not-special but hidden) that don't show when I print.

length($var)  <--- Returns a number 10-15 larger than "somestring" has chars

I can eliminate the special characters like so:

$var =~ s/[^[:print:]]+//g

But it appears that there are also non-special characters that are revealed once the special characters have been removed:

print $var   <---- Displays "0;<hostname>somestring" 
            (where <hostname> is the system hostname)

Is there a way to eliminate both the special characters AND the non-special characters that were being hidden? I want to be able to use $var as the key of a hash, and then reference it by "somestring"

$hash{$var} = 123
print $hash{'somestring'} 

Thoughts?

like image 255
Jonathan Avatar asked Aug 27 '12 08:08

Jonathan


1 Answers

Can we assume that the characters you want to remove are before the non-printable characters ?

If so, maybe something like

$var =~ s/.*[^[:print:]]+//;

could work ?

like image 109
Orabîg Avatar answered Oct 22 '22 12:10

Orabîg