stringperloctalstringescapeutils

How can I convert octal character values in a mixed string with Perl?


I require to use Perl (v5.10) to process strings from another application with embedded octal values for spaces, commas and other non-alphanumeric characters.

For example: This - "11624\0040SE\00405th\0040St\0054\0040Suite\0040100", should be this - "11624 SE 5th St, Suite 100".

I can accomplish what I'm looking for at the Linux command line with "echo -e", but I need to be able to process and manipulate in a Perl script.

echo -e "11624\0040SE\00405th\0040St\0054\0040Suite\0040100"

Output:

11624 SE 5th St, Suite 100

I've looked at the String::Escape module, but it doesn't seem to do what I'm thinking it should.

use String::Escape qw(backslash unbackslash);

my $strval = "11624\0040SE\00405th\0040St\0054\0040Suite\0040100";
my $output = unbackslash($strval);
printf("%s\n", $strval);
printf("%s\n", $output);

I've done numerous Google and Stack Overflow searches for similar questions/answers and have not yet come across any.


Solution

  • This type of thing is generally easy enough to do in a regex:

    $ perl -E 'say shift =~ s[\\0([0-7]{1,3})][chr oct $1]egr' '11624\0040SE\00405th\0040St\0054\0040Suite\0040100' 11624 SE 5th St, Suite 100

    Probably easier that way.