NEWBIE:Compare if value of variable has changed since last time thru a loop

M

Mista_Blista

Hello:

I am analyzing boot logs for a report. The lines in the log I am concerned with look like:

Oct 2 14:56:09 bootpd: info(6): DNCS CAQAM[qam_172.16.31.76:ht=ethernet:sm=255.255.255.0:ip=172.16.31.76:ha=0x0002DE821E07:

This is the code I have so far:
#!/usr/bin/perl -w
while($line = <STDIN>) {

if($line =~ /CAQAM/) {
$line =~ s/qam_/qam_ /g;
$line =~ s/:ht=/ :ht=/g;
@columns = split(/\s+/, $line);
$boot_entry = ("$columns[0]" . ' ' . "$columns[1]" . ' ' . "$columns[2]" . ' ' . "$columns[7]" . "\n");
push(@output, $boot_entry);
}
}

Below is an example of a line of output:
Oct 13 17:21:15 172.16.35.35

Below is what'd I'd like to achieve. The main question I pose is how to store the previous date and only begin a new column when the date has changed? I am hacking away at the llama and the camel, man even the dummies book but I need this report automated very soon.

Oct 2,Oct 4
14:56:09 172.16.31.76,13:33:38 172.16.34.200
14:56:11 172.16.31.74,14:08:36 172.16.32.198

Thanks for any feedback at all. Even flames
 
F

fifo

At said:
I am analyzing boot logs for a report. The lines in the log I am concerned with look like:

Oct 2 14:56:09 bootpd: info(6): DNCS CAQAM[qam_172.16.31.76:ht=ethernet:sm=255.255.255.0:ip=172.16.31.76:ha=0x0002DE821E07: [snip]

Below is what'd I'd like to achieve. The main question I pose is how
to store the previous date and only begin a new column when the date
has changed? I am hacking away at the llama and the camel, man even
the dummies book but I need this report automated very soon.

Oct 2,Oct 4
14:56:09 172.16.31.76,13:33:38 172.16.34.200
14:56:11 172.16.31.74,14:08:36 172.16.32.198

If you want the output in columns like this, you have to read in what
you want from the file into some sort of data structure before you can
start the output. I'd probably do something like this:

#!/usr/bin/perl

use strict;

my @data;
my $last;
my $max_num = 0;

while (<>) {
next unless /CAQAM/;
my ($month, $day, $time) = split /\s+/;
my ($ip) = /:ip=([\d.]+):/;

unless (defined $last and $last->{month} eq $month
and $last->{day} eq $day) {
push @data, { month => $month, day => $day, records => [ ] };
$last = $data[$#data];
}

my $num = push @{$last->{records}}, { time => $time, ip => $ip };
$max_num = $num if $num > $max_num;
}

$\ = "\n";

print join ',', map { "$_->{month} $_->{day}" } @data;

for my $i (0..($max_num-1)) {
print join ',', map { "$_->{time} $_->{ip}" }
map { $_->{records}[$i] } @data;
}
__END__
 
M

Mista_Blista

fifo said:
At said:
I am analyzing boot logs for a report. The lines in the log I am concerned with look like:

Oct 2 14:56:09 bootpd: info(6): DNCS
CAQAM[qam_172.16.31.76:ht=ethernet:sm=255.255.255.0:ip=172.16.31.76:ha=0x000
2DE821E07:
[snip]

Below is what'd I'd like to achieve. The main question I pose is how
to store the previous date and only begin a new column when the date
has changed? I am hacking away at the llama and the camel, man even
the dummies book but I need this report automated very soon.

Oct 2,Oct 4
14:56:09 172.16.31.76,13:33:38 172.16.34.200
14:56:11 172.16.31.74,14:08:36 172.16.32.198

If you want the output in columns like this, you have to read in what
you want from the file into some sort of data structure before you can
start the output. I'd probably do something like this:

#!/usr/bin/perl

use strict;

my @data;
my $last;
my $max_num = 0;

while (<>) {
next unless /CAQAM/;
my ($month, $day, $time) = split /\s+/;
my ($ip) = /:ip=([\d.]+):/;

unless (defined $last and $last->{month} eq $month
and $last->{day} eq $day) {
push @data, { month => $month, day => $day, records => [ ] };
$last = $data[$#data];
}

my $num = push @{$last->{records}}, { time => $time, ip => $ip };
$max_num = $num if $num > $max_num;
}

$\ = "\n";

print join ',', map { "$_->{month} $_->{day}" } @data;

for my $i (0..($max_num-1)) {
print join ',', map { "$_->{time} $_->{ip}" }
map { $_->{records}[$i] } @data;
}
__END__


thanks a bunch. I am determined to learn what made this work. thank you
again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top