Ok, theres a log file containing the below. Lets call the logfile log_fantastic:
2009/03/16 21:42:45 USER: tonnabo - MAC: 0014BF2D385A - STATUS_ID: 30 - STATE: ERROR
2009/03/16 21:42:45 USER: tonnabo - MAC: 001310AC120D - STATUS_ID: 15 - STATE: OK
2009/03/16 21:42:45 USER: tonnabo - MAC: 001DD9277095 - STATUS_ID: 20 - STATE: ERROR
I want to write a perl script that will read the contents of log_fantastic and output only the lines that does not have a status_id of 10?
the below is what i've done. not sure if this is correct:
use Data:: Dumper;
main();
sub main {
my @results ;
my $headers = "user,
mac address,
status_id";
my (@log_info , @dircontents );
open (LOG, "</home/jhenson/log_fantastic");
@log_info = <LOG>;
close (LOG);
print Dumper("testing");
foreach my $content (@log_info ) {
chomp($content);
my $line = substr ($content, -2);
print Dumper($line);
if ($line eq "OK") {
push @dircontents , $content;
}
}
#push @alarm_info , uc $headers;
print Dumper(@dircontents );
}
curleb
March 17, 2009, 2:46pm
2
you need a perl script to do this? :
grep -v 'STATUS_ID: 10 ' ${file:-"/home/jhenson/log_fantastic"}
Or is there more to it...?
Try the perl script : -
#!/usr/bin/perl
open(FH,"log_fantastic") or dir ("Couldnt open file");
while(<FH>)
{
$rec=$_;
chomp ($rec);
@arr=split(/:/,$rec);
print $rec,"\n" if ($arr[5]!~/10/);
}
close(FH);
curleb:
you need a perl script to do this? :
grep -v 'STATUS_ID: 10 ' ${file:-"/home/jhenson/log_fantastic"}
Or is there more to it...?
oh theres more to it. i coulda easily done this through shell programming. but this needs to be done in perl. on top of grepping out the wanted status ids, i goto set it up in fields. thanks a million for your suggestion
jambesh:
Try the perl script : -
#!/usr/bin/perl
open(FH,"log_fantastic") or dir ("Couldnt open file");
while(<FH>)
{
$rec=$_;
chomp ($rec);
@arr=split(/:/,$rec);
print $rec,"\n" if ($arr[5]!~/10/);
}
close(FH);
this works PERFECTLY. thanks a million.
This should be more efficient and more accurate:
open(FH,"log_fantastic") or dir ("Couldnt open file: $!");
while(my $line = <FH>) {
next if (/STATUS_ID: 10 /);
print;
}
close(FH);