druisgod has asked for the wisdom of the Perl Monks concerning the following question:
The script is supposed to open a file, print the current date to it, close the file, open another file parse out data that I want from access.log and then loop to the next iteration. What I'm seeing in the files that it creates is the data first and the date on the bottom. I'm just beginning with perl and I am used to writing shell scripts. I can't figure out why the data gets printed before the date. Anyone?
#!/usr/bin/perl -w use diagnostics; #This file parses out squids access log for address bar entries to see + where users purposefully go $date=`date`; $net="192.168.4."; $counter=10; $logpath="/var/log/squid/userarchive/"; $ip=$net.$counter; #The counter is set to the max ipadress +1 while ( $counter <= 14 ) { #Print the date to each file we're writing to open(LOGF,">>",$logpath . $ip); print LOGF "$date\n"; $|++; close LOGF; #Grab the access log and go through it, splitting each line into eleme +nts of the array open(INFILE,"/var/log/squid/access.log") or die "Can’t open /v +ar/log/squid/access.log: $!"; while ( <INFILE> ) { @array = split(" ",); $ip=$net.$counter; #Only grab things associated with the current searched for ip and ends + with .com .net and so on if (($array[7] =~ /[com|org|net|gov|cc|mil]\/$ +/)&&($array[3] eq $ip)) { #Write it to the log file open(LOGF,">>",$logpath . $ip) or die +"Can't open $logpath$ip: $!"; print LOGF "$ip \: $array[0] +$array[7]\n"; close LOGF; } } $counter++; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl printing in the wrong order
by Eliya (Vicar) on Apr 29, 2011 at 12:47 UTC | |
by druisgod (Initiate) on Apr 29, 2011 at 13:27 UTC | |
|
Re: Perl printing in the wrong order
by JavaFan (Canon) on Apr 29, 2011 at 12:57 UTC | |
|
Re: Perl printing in the wrong order
by jwkrahn (Abbot) on Apr 29, 2011 at 13:20 UTC | |
|
Re: Perl printing in the wrong order
by Gulliver (Monk) on Apr 29, 2011 at 13:21 UTC | |
|
Re: Perl printing in the wrong order
by Anonymous Monk on Apr 29, 2011 at 14:36 UTC | |
by druisgod (Initiate) on Apr 29, 2011 at 16:47 UTC |