Someone I know is writing a little script to parse a PIX fiewall log, grab elements from each line, and insert said elements into a database. Here's a sample of the data:
conduit permit tcp host 192.168.1.1 eq www any (hitcnt=57476) conduit permit tcp host 192.168.1.1 eq 139 host 192.168.2.1 (hitcnt=2)
What he wants to end up with is:
conduit permit tcp|host|192.168.1.1|eq|www|any|(hitcnt=57476) conduit permit tcp|host|192.168.1.1|eq|139|host|192.168.2.1|(hitcnt=2)
Forget about writing to the database, for now. The more immediate problem is getting the program to "create" variables on the fly, depending upon how many "elements" a line contains. Assuming that this is even the right way to approach this problem; I don't know of any other way. Hence, this post. =)
Here's the code h/we have come up with. Please contain the laughter, for now.
#!/usr/bin/perl -w use strict; my $in = "/home/trix/test"; my $out = "/home/trix/out"; use vars qw(@array @list); use vars qw ($host1 $host2 $conduit $permit $tcpudp $ip1 $ip2 $eq $port $hitcnt $line); open IN, "$in" || die "$!\n"; open OUT, ">$out" || die "$!\n"; while ($line = <IN>) { chomp $line; ($conduit, $permit, $tcpudp, $host1, $ip1, $eq, $port, $host2,$ip2 +, $hitcnt)=split( /\s/, $line); print OUT "$conduit $permit $tcpudp $host1\|$ip1\|$eq\|$port\|$hos +t2\|$ip2\|$hitcnt\n"; }
This prints:
conduit permit tcp|host|192.168.1.1|eq|www|any|(hitcnt=57476) conduit permit tcp|host|192.168.1.1|eq|139|host|192.168.2.1
I thought that this was going to be a pretty easy thing to accomplish (for me), but I must say that I'm stumped. And it's not even my work! But I'm posting this, because I really want to learn how to do this. It seems like this could be pretty useful stuff.

Thanks,
Tuna

In reply to Variable number of words/fields in a line/record by Tuna

Title:
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, details, 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, summary, 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.