I have patterns in a file which looks like this:
|30 14 06 03 55 04 03 14 0D 2A|.dropbox.com
User-Agent|3A|Google|20 20|Desktop
The data between "|" need to be saved as it is without the spaces and data outside pipes need to be converted to ASCII.
I tried to use regexes but wasn't successful, so I tried character by character approach. But I'm not able to achieve the goal. The code is as below
#!/usr/bin/perl
use strict;
#use warnings;
sub ascii_to_hex ($)
{
## Convert each ASCII character to a two-digit hex number.
(my $str = shift) =~ s/(.|\n)/sprintf("%02lx", ord $1)/eg;
return $str;
}
my @a_out;
my @a_str;
my @b_str;
my $str;
my $index="0";
my $count;
my $flag;
my $file= 'rules.txt';
my $statenum="0";
my $num="0";
my $file1= 'XXX.txt';
my $file2= 'happ.txt';
my $file3= 'hex.txt';
my $match=quotemeta ("|");
my $space=quotemeta (" ");
open INFILE, "<$file"
or die "can't open file: $!";
open OUTFILE2, ">$file3"
or die "can't open file: $!";
open OUTFILE, ">$file1"
or die "can't open file: $!";
open OUTFILE1, ">$file2"
or die "can't open file: $!";
while( my $line = <INFILE> )
{
$num=$num+1;
my $digits='0';
my $index2=length($line);
print ("Processing Rule",$num,"\n");
@a_str = split(//, $line);
print (OUTFILE1 @a_str,"\n");
foreach(@a_str)
{ chomp($_);
print(OUTFILE2 $a_str[$index],"\n");
print(($a_str[$index]),"\n");
if ($count=='0')
{
if (($a_str[$index])==$match)
{$count='1'; ("count=",$count,"START OF PIPE:",$a_str[$index],
+"\n");}
elsif (($a_str[$index])==$space)
{ print (OUTFILE $a_str[$index]);print ("SAVING ASCII SPACE:",
+$a_str[$index],"\n");}
else { my $hex = ascii_to_hex($a_str[$index]);
print (OUTFILE $hex); print ("SAVING ASCII ELEMENTS OU
+TSIDE PIPE",$hex,"\n");
}
}
elsif($count=='1')
{
if (($a_str[$index])==$match){ $count='0'; print("count=",$coun
+t,"END OF PIPE:", $a_str[$index],"\n");}
elsif (($a_str[$index])==$space){ print ($a_str[$index],"SKIPPI
+NG BINARY SPACE","\n");}
else {
print (OUTFILE $a_str[$index]); print ("SAVING ELEMENTS IN BET
+W PIPE",$a_str[$index],"\n");
}
}
$index=$index+1;
print($index,"\t" );
}
print ("\n");
print (OUTFILE1 "\n");
print (OUTFILE "\n");
print (OUTFILE2 "\n");
}
close INFILE
or die "can't close file: $!";
close OUTFILE1
or die "can't close file: $!";
close OUTFILE2
or die "can't close file: $!";
close OUTFILE
or die "can't close file: $!";