From: jesse_hardy on
I have log files that I want to parse through every day and search for
failures and errors. I understand how to open the file and search for
failures and errors but I may have several lines that have the same
error. What I want to do is print the first occurence to a file and
count each occurence after the first and print that to the same file.
So if I had 50 lines with the same error I wanted to print the first
error to a file and say there were 49 more occurences of that error.

Thanks
Jesse

From: Paul Lalli on
On Feb 6, 2:56 pm, jesse_ha...(a)premierinc.com wrote:
> I have log files that I want to parse through every day and search for
> failures and errors. I understand how to open the file and search for
> failures and errors but I may have several lines that have the same
> error. What I want to do is print the first occurence to a file and
> count each occurence after the first and print that to the same file.
> So if I had 50 lines with the same error I wanted to print the first
> error to a file and say there were 49 more occurences of that error.

So what have you tried so far, and where did it go wrong?

I would use a hash to keep track of the errors and how many more there
are
[untested]
my %count_of;
while (<DATA>) {
if (/Error: (.*)/) {
$count_of{$1}++;
}
}
for my $error (keys %count_of) {
print "There were $count_of{$error} instances of $error\n";
}


If that's not what you're looking for, please post a short-but-
complete script that demonstrates your problem, along with sample
input and desired output.

(Have you read the Posting Guidelines that are posted here twice a
week?)

Paul Lalli

From: jesse on
On Feb 6, 3:22 pm, "Paul Lalli" <mri...(a)gmail.com> wrote:
> On Feb 6, 2:56 pm, jesse_ha...(a)premierinc.com wrote:
>
> > I have log files that I want to parse through every day and search for
> > failures and errors. I understand how to open the file and search for
> > failures and errors but I may have several lines that have the same
> > error. What I want to do is print the first occurence to a file and
> > count each occurence after the first and print that to the same file.
> > So if I had 50 lines with the same error I wanted to print the first
> > error to a file and say there were 49 more occurences of that error.
>
> So what have you tried so far, and where did it go wrong?
>
> I would use a hash to keep track of the errors and how many more there
> are
> [untested]
> my %count_of;
> while (<DATA>) {
> if (/Error: (.*)/) {
> $count_of{$1}++;
> }}
>
> for my $error (keys %count_of) {
> print "There were $count_of{$error} instances of $error\n";
>
> }
>
> If that's not what you're looking for, please post a short-but-
> complete script that demonstrates your problem, along with sample
> input and desired output.
>
> (Have you read the Posting Guidelines that are posted here twice a
> week?)
>
> Paul Lalli

Paul, I have a question. The (.*) is supposed to capture everything
after the match right? So if I didn't want to capture everything
after the match could this be changed some how to just grab the next
35 characters after the match or before the match?

From: Paul Lalli on
On Feb 14, 10:20 am, "jesse" <jesse_ha...(a)premierinc.com> wrote:
> On Feb 6, 3:22 pm, "Paul Lalli" <mri...(a)gmail.com> wrote:

> > while (<DATA>) {
> > if (/Error: (.*)/) {
> > $count_of{$1}++;
> > }}

> Paul, I have a question. The (.*) is supposed to capture everything
> after the match right? So if I didn't want to capture everything
> after the match could this be changed some how to just grab the next
> 35 characters after the match or before the match?

Sure it could.

if (/Error: (.{35})/) {

or

if (/(.{35}) Error:/) {

Please read up on regular expressions:
perldoc perlre
perldoc perlretut
perldoc perlreref

Paul Lalli


From: jesse on
On Feb 14, 10:37 am, "Paul Lalli" <mri...(a)gmail.com> wrote:
> On Feb 14, 10:20 am, "jesse" <jesse_ha...(a)premierinc.com> wrote:
>
> > On Feb 6, 3:22 pm, "Paul Lalli" <mri...(a)gmail.com> wrote:
> > > while (<DATA>) {
> > > if (/Error: (.*)/) {
> > > $count_of{$1}++;
> > > }}
> > Paul, I have a question. The (.*) is supposed to capture everything
> > after thematchright? So if I didn't want to capture everything
> > after thematchcould this be changed some how to just grab the next
> > 35charactersafter thematchorbeforethematch?
>
> Sure it could.
>
> if (/Error: (.{35})/) {
>
> or
>
> if (/(.{35}) Error:/) {
>
> Please read up on regular expressions:
> perldoc perlre
> perldoc perlretut
> perldoc perlreref
>
> Paul Lalli

Perfect Paul! Thanks for your help, and I will read those perldocs.