perlnewbie9292
Programmer
Hello everyone,
I am trying to normalize some data files that we generate which have a mix of ascii and hex chars in the header lines. I am new to Perl and having problems figuring out why 1) I am only able to grab the first hex match in each line instead of all/every occurrence of a hex value in a line and 2) I have no idea how to substitute the converted string back into the line or just print it, I don't need to have it written back to the file just need to print out what it would look like once the hex has been converted to the correct char value. Not sure if I am going about this the right way and probably over complicating things. I've been looking at this for a few hours and just getting frustrated. Any pointers/samples on how to go about this would be greatly appreciated.
thanks for the help
-----Data in Test File--------
-------OutPut received---------
------desired output--------
I am trying to normalize some data files that we generate which have a mix of ascii and hex chars in the header lines. I am new to Perl and having problems figuring out why 1) I am only able to grab the first hex match in each line instead of all/every occurrence of a hex value in a line and 2) I have no idea how to substitute the converted string back into the line or just print it, I don't need to have it written back to the file just need to print out what it would look like once the hex has been converted to the correct char value. Not sure if I am going about this the right way and probably over complicating things. I've been looking at this for a few hours and just getting frustrated. Any pointers/samples on how to go about this would be greatly appreciated.
thanks for the help
Code:
#!/usr/bin/perl
use strict;
use warnings;
my $hexByte;
my $hex;
my $str;
sub hTOa ($);
my $file = $ARGV[0];
open(FILE, $file) || die "Could not open file: $file $!\n";
while ( <FILE> ) {
my $line = $_;
(my $lineToDecode = $line) =~ /(FileExport##(.+?(#[\d]{2}).+?(#[\d]{2}).+?).+?##endFileExport)/gmi;
foreach ( $lineToDecode ) {
my $lineWithHex = $1;
print "Matched FileExport Line here: $lineWithHex\n";
foreach ( $lineWithHex ) {
my $hexChars = $lineWithHex;
$hexChars =~ /(#[0-9a-fA-F]{2})/gmi;
$hex = $1;
$hex =~ s/#//gm;
hTOa $hex;
my $newLine = s/(FileExport##(.+?(#[\d]{2}).+?(#[\d]{2}).+?).+?##endFileExport)/(FileExport##(.+?($str).+?($str).+?).+?##endFileExport)/gm;
#print "Replaced Hex Values $newLine\n\n\n";
}
}
}
sub hTOa ($) {
($str = shift) =~ s/([a-fA-F0-9]{2})/chr(hex $hex)/eg;
print "Converted String: $str\n\n";
return $str;
}
-----Data in Test File--------
Code:
FileExport##/#74#65#73#74 #65#6ecoded #6c#69#6ee ##endFileExport
FileExport##/#74#65#73#74 #65#6ecoded #6c#69#6ee Number 2 ##endFileExport
FileExport##/#74#65#73#74 #65#6ecoded #6c#69#6ee #4e#75#6d#62#65#72 3 ##endFileExport
-------OutPut received---------
Code:
Matched FileExport Line here: FileExport##/#74#65#73#74 #65#6ecoded #6c#69#6ee ##endFileExport
Converted String: t
Matched FileExport Line here: FileExport##/#74#65#73#74 #65#6ecoded #6c#69#6ee Number 2 ##endFileExport
Converted String: t
Matched FileExport Line here: FileExport##/#74#65#73#74 #65#6ecoded #6c#69#6ee #4e#75#6d#62#65#72 3 ##endFileExport
Converted String: t
------desired output--------
Code:
FileExport##/test encoded line ##endFileExport
FileExport##/test encoded line Number 2 ##endFileExport
FileExport##/test encoded line Number 3 ##endFileExport