Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help replacin deci to eq Char string in a line

Status
Not open for further replies.

perlnewbie9292

Programmer
Jul 15, 2009
25
US
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

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
 
I think you're looking for something more like this:

Code:
my @lineToDecode = ($line =~ /(FileExport##(.+?(#[\d]{2}).+?(#[\d]{2}).+?).+?##endFileExport)/gi;)

i.e. you need to use an array for the match to return multiple values. The "m" switch is not required since you are processing the input line-by-line anyway.

Personally I would use a while match loop something like this:

Code:
[gray]#!/usr/bin/perl[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$file[/blue] = [blue]$ARGV[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red][red];[/red]
[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red]FILE, [blue]$file[/blue][red])[/red] || [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Could not open file: [blue]$file[/blue] [blue]$![/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[olive][b]while[/b][/olive] [red]([/red] <FILE> [red])[/red] [red]{[/red]
        [black][b]my[/b][/black] [blue]$suffix[/blue][red];[/red]
        [olive][b]while[/b][/olive] [red]([/red][red]/[/red][purple](.*?)#([[:xdigit:]]{2})[/purple][red]/[/red][red]g[/red][red])[/red] [red]{[/red]
                [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$1[/blue][/purple][red]"[/red] . [url=http://perldoc.perl.org/functions/chr.html][black][b]chr[/b][/black][/url][red]([/red][url=http://perldoc.perl.org/functions/hex.html][black][b]hex[/b][/black][/url][red]([/red][blue]$2[/blue][red])[/red][red])[/red][red];[/red]
                [blue]$suffix[/blue]=[blue]$'[/blue][red];[/red]
        [red]}[/red]
        [olive][b]if[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/defined.html][black][b]defined[/b][/black][/url] [blue]$suffix[/blue][red])[/red] [red]{[/red] [black][b]print[/b][/black] [blue]$suffix[/blue] [red]}[/red] [olive][b]else[/b][/olive] [red]{[/red] [black][b]print[/b][/black] [red]}[/red][red];[/red]
[red]}[/red]

... but you may prefer to get your original code working by trying what I suggest above.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top