Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

help with splitting and printing the array

by perlnewbie012215 (Novice)
on Jan 24, 2015 at 19:36 UTC ( [id://1114382]=perlquestion: print w/replies, xml ) Need Help??

perlnewbie012215 has asked for the wisdom of the Perl Monks concerning the following question:

Hi All Monks,

I am pretty new to perl and I wrote a small perl script to ease my pain, did not work.

My input looks like this with over 20 line in the text file.

18:06:04.616 [79952.57692] <2> logparams: -S server1.prod.com -D serve +r2.prod.com -C server3.prod.com -X -s 1421467204 -e 1421467204 -R E:\ +Program Files\725635\logs\log.tmp.chg -K -t 13 -M -k -L E:\Program F +iles\725635\logs\infod.tmp.log -W -f E:\Program Files\725635\logs\aft +er.tmp 17:49:34.216 [58140.81564] <2> logparams: -S -l -a -k server1.prod.com + -D client10.prod.com -C sql12.prod.com -X -s 1421467205 -e 142146720 +5 -R E:\Program Files\561234\logs\log.tmp.chg -K -t 13 -M -k -L E:\P +rogram Files\561234\infod10.tmp.log -W -f E:\Program Files\561234\log +s\after.tmp

and my output should look like this:

server1.prod.com,server2.prod.com,server3.prod.com,725635,infod.tmp.lo +g server1.prod.com,client10.prod.com,sql12.prod.com,561234,infod10.tmp.l +og

My script look like this, it worked only when the count is right as you can see the second line the count went way off, the fields count in my code is not accurate but you get the point. I need a different logic, can you guys help me out.. Thank you all

open(FILE, $filename) or die "Could not read from $filename, program h +alting."; while(<FILE>) { # get rid of the pesky newline character chomp; # read the fields in the current record into an array @fields = split(/[\s\\]+/, $_); $datestring = localtime(); open logreport, ">>E:\\temp\\logs\\eligiblereport.txt"; print logreport "$date,$fields[5],$fields[7],$fields[9],$fields[24] +,$fields[43]\n"; } close logreport; close FILE;

Replies are listed 'Best First'.
Re: help with splitting and printing the array
by blindluke (Hermit) on Jan 24, 2015 at 20:54 UTC

    Hello, perlnewbie012215, welcome to the Monastery.

    First of all, start using use strict; use warnings; This would help you with the mistake of storing localtime in $datestring and trying to print $date.

    Your biggest problem is the fact that you are splitting the log line into words (roughly), and you are counting that the server1.prod.com will be the sixth word. You already know what happens if some additional parameters show up before the server name - the count is off. But what happens when someone switches the order of parameters? Try using a regular expression to match the specific parameters, and not counting which word is the server name.

    UPDATE:

    Take a look at this small script operating on your sample data. This should hopefully set you on the right track.

    #!/usr/bin/perl use warnings; use strict; while(<DATA>) { chomp; my $servname = '[a-z0-9]+\.[a-z0-9]+\.[a-z0-9]+'; /-S.+?($servname).+-D.+?($servname).+-C.+?($servname).+$/; print "Server after -S: $1; Server after -D: $2; Server after -C: +$3;\n"; } __DATA__ 18:06:04.616 79952.57692 <2> logparams: -S server1.prod.com -D server2 +.prod.com -C server3.prod.com -X -s 1421467204 -e 1421467204 -R E:\Pr +ogram Files\725635\logs\log.tmp.chg -K -t 13 -M -k -L E:\Program File +s\725635\logs\infod.tmp.log -W -f E:\Program Files\725635\logs\after. +tmp 17:49:34.216 58140.81564 <2> logparams: -S -l -a -k server1.prod.com - +D client10.prod.com -C sql12.prod.com -X -s 1421467205 -e 1421467205 +-R E:\Program Files\561234\logs\log.tmp.chg -K -t 13 -M -k -L E:\Prog +ram Files\561234\infod10.tmp.log -W -f E:\Program Files\561234\logs\a +fter.tmp

    The output:

    Server after -S: server1.prod.com; Server after -D: server2.prod.com; +Server after -C: server3.prod.com; Server after -S: server1.prod.com; Server after -D: client10.prod.com; + Server after -C: sql12.prod.com;

    - Luke

      After splitting the line you can simply get rid of the fields starting with '-' and then print the appropriate array elements.
      while (<DATA>) { chomp; my @line = grep {!/^-/} split; print $line[0], ' ' , $line[4], "\n"; }

        And how will that help when someone switches the order of parameters? In my reply, I've stated that there are two big problems with the split approach. OP already encountered the first one, but your proposed solution won't save him from the second. Relying on the order of commandline parameters is a wrong approach here.

        - Luke

      Thank you Luke!! That helped me out alot.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1114382]
Approved by bitingduck
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-29 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found