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

Know you guys are busy, but if I may have a moment of your time perhaps someone can help out a new to perl guy.

Situation: I'm writing a simple script that parses a syslog file full of data that looks like this:

# Aug 5 11:50:50 ev-cis-cons2 142682254: 2004 Aug 05 11:50:50.040 EDT + -4:00 %AUTH-5-28: RPT=88653: 10.1.1.1: User [user0] Group [group0] d +isconnected: Duration: 1:16:40 Bytes xmt: 1441872 Bytes rcv: 56377 +6 Reason: User Requested # Aug 5 11:51:52 ev-cis-cons2 142683959: 2004 Aug 05 11:51:52.760 EDT + -4:00 %AUTH-5-28: RPT=88654: 10.1.1.2: User [user1] Group [group1] d +isconnected: Duration: 3:55:38 Bytes xmt: 21588288 Bytes rcv: 2879 +544 Reason: Lost Service # Aug 5 11:52:22 ev-cis-cons2 142685133: 2004 Aug 05 11:52:22.650 EDT + -4:00 %AUTH-5-28: RPT=88655: 10.1.1.3: User [user2] Group [group2] d +isconnected: Duration: 1:35:37 Bytes xmt: 1424368 Bytes rcv: 31430 +4 Reason: User Requested

(Reluctant editorial note from Ovid: Fixed this again because we were still getting complaints. The above log info is now in <code> tags because it was still not wrapping well for people. Just note that the '# ' at the beginning of each line was not in the original.)

My script:

#Look in daily syslog file and grab the #user, group, connection duration, bytes xmt and bytes rcv #Stick this data into a daily report in .csv format # #TIME & DATE STUFF $stime = localtime; @time1 = split(' ', $stime); $mon = $time1[1]; $day = $time1[2]; $year = $time1[4]; $filename = "dailyreport-$mon-$day-$year.csv"; #Open the log file for reading open (LOG, "</var/log/ciscon.log") || die "Couldn't open log file $!"; open (TMP, ">>tmpfile.txt") || die "Couldn't open tmp file $!"; open (DREPORT, ">>/root/reports/$filename") || die "Couldn't open csv +$!"; print DREPORT "DAILY LOG FOR $mon $day $year\n"; print DREPORT "\n"; print DREPORT "USERNAME,GROUP,DURATION,BYTES XMT,BYTES RCV,\n"; print DREPORT "\n"; #Pump data into array and parse for usefull data #Usefull data goes into tmpfile while( <LOG>) { @tmp = grep {/disconnected/} $_; print TMP "@tmp"; } #Close stuff we dont need close TMP; close LOG; #Open temp file and stuff into array by spaces print data we #want into the daily csv file open (TMP1, "<tmpfile.txt") || die "Can't read from tmpfile $!"; while( <TMP1> ) { chomp $_; @tmpa = split(' ', $_); print DREPORT "$tmpa[15],$tmpa[17],$tmpa[20],$tmp[23],$tmp[26] +\n"; $cnt++; } close TMP1; unlink ("tmpfile.txt");

Everything works with the exception of bytes xmt/rcv, it seems the data doesn't show up for some reason. Here's an example of the output I get:


[user1],[group1],0:09:19,,

[user2],[group2],0:27:21,,

[user3],[group3],7:07:09,,

Thanks for your time/help.

Edited 2004-08-05 by Ovid

Replies are listed 'Best First'.
Re: Can't get data out of array
by Tomte (Priest) on Aug 05, 2004 at 16:27 UTC

    Always use strict; and use warnings; if you are developing, helps a lot:

    This a little bit "strictified" version of your code

    #!/bin/env perl #Look in daily syslog file and grab the #user, group, connection duration, bytes xmt and bytes rcv #Stick this data into a daily report in .csv format # use warnings; use strict; #TIME & DATE STUFF my $stime = localtime; my @time1 = split(' ', $stime); my $mon = $time1[1]; my $day = $time1[2]; my $year = $time1[4]; my $filename = "dailyreport-$mon-$day-$year.csv"; #Open the log file for reading open (LOG, "</var/log/ciscon.log") || die "Couldn't open log file $!"; open (TMP, ">>tmpfile.txt") || die "Couldn't open tmp file $!"; open (DREPORT, ">>/root/reports/$filename") || die "Couldn't open csv +$!"; print DREPORT "DAILY LOG FOR $mon $day $year\n"; print DREPORT "\n"; print DREPORT "USERNAME,GROUP,DURATION,BYTES XMT,BYTES RCV,\n"; print DREPORT "\n"; #Pump data into array and parse for usefull data #Usefull data goes into tmpfile while( <LOG>) { my @tmp = grep {/disconnected/} $_; print TMP "@tmp"; } #Close stuff we dont need close TMP; close LOG; #Open temp file and stuff into array by spaces print data we #want into the daily csv file open (TMP1, "<tmpfile.txt") || die "Can't read from tmpfile $!"; while( <TMP1> ) { chomp $_; my @tmpa = split(' ', $_); print DREPORT "$tmpa[15],$tmpa[17],$tmpa[20],$tmp[23],$tmp[26] +\n"; } close TMP1; unlink ("tmpfile.txt");
    yields:
    [1823]tom@margo perl $ perl -c test3.pl Global symbol "@tmp" requires explicit package name at test3.pl line 5 +0. Global symbol "@tmp" requires explicit package name at test3.pl line 5 +0. test3.pl had compilation errors.
    I guess you mean tmpa in both cases, but I may be wrong...

    regards,
    tomte


    An intellectual is someone whose mind watches itself.
    -- Albert Camus

      Ok, I'm a moron for missing that. You can bet I'll have use strict and warnings from now on. Sheesh. Thanks!