Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I have patterns in a file which looks like this:

|30 14 06 03 55 04 03 14 0D 2A|

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: $!";

In reply to Problem while Data Extraction by jpvinny28

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (6)
    As of 2018-04-21 06:27 GMT
    Find Nodes?
      Voting Booth?