zim
-
Registered: Dec 2002
Location:
Posts: 3063 |
another possibility:
quote:
#!/usr/bin/perl -w
use strict;
my $inputString = shift;
my @fields = split /,/, $inputString;
print "Printing fields: " . join(', ', @fields). "\n";
while(<STDIN> )
{
chomp;
my %data = split /[ =]/, "protocol=$_";
my @output;
foreach my $field (@fields)
{
push(@output, $data{$field}) if(defined $data{$field});
}
next unless(@output);
print join(',', @output) . "\n";
}
use:
cat log | ./parse.pl protocol,src,dst
output:
Printing fields: protocol, src, dst
httpd,192.168.1.1/4675,195.224.113.251/80
httpd,192.168.1.1/4675,192.168.2.1/80
if you ask for something that doesnt exist, you get nothing.
Printing fields: protocol, joe, src, dst
httpd,192.168.1.1/4675,195.224.113.251/80
httpd,192.168.1.1/4675,192.168.2.1/80
if you want a null field in that case, you can change the foreach loop to include:
quote:
if (defined $data{$field})
{
push(@output, $data{$field});
}
else
{
push(@output, q{});
}
instead.
resulting output would be:
Printing fields: protocol, joe, src, dst
httpd,,192.168.1.1/4675,195.224.113.251/80
httpd,,192.168.1.1/4675,192.168.2.1/80
__________________
insert witty remark
Last edited by CHiPsJr on 11-09-2006 at 08:23 AM
Report this post to a moderator |
IP: Logged
|