Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Counting the number of times several strings appear

by cspctec (Sexton)
on Nov 13, 2012 at 16:53 UTC ( #1003664=perlquestion: print w/ replies, xml ) Need Help??
cspctec has asked for the wisdom of the Perl Monks concerning the following question:



Hello, I have an array that I have dumped a file into. The file contains several hundred different sentences.


The array contains, for example:

User logged in successfully at 10:35:33

User logged in successfully at 10:35:34

User logged in successfully at 10:35:35

Error: Login failure!

User logged in successfully at 10:35:37

Error: Login failure!

Error: System failure at 01:23:22

Error: System failure at 01:23:40

User logged in successfully at 10:35:39

Error: System failure at 01:23:41

So... what I've been trying to do is create a perl script to tell me how many times each error occurred.

I would need the output to look like:

User logged in successfully at 10:35:33
The above event occurred 4 additional times
Error: Login failure!
The above event occurred 1 additional times
Error: System failure at 01:23:22
The above event occurred 2 additional times

I have been trying to figure out a way to make the script count each event individually, but the for loops I have been using are only counting the first few matches.

Can someone come up with an algorithm that can count these and output the results like I have listed? Thanks for any help you can provide!

Comment on Counting the number of times several strings appear
Re: Counting the number of times several strings appear
by Kenosis (Priest) on Nov 13, 2012 at 17:01 UTC

    Hi, cspctec, and welcome to PerlMonks!

    ...what I've been trying to do is create a perl script to tell me how many times each error occurred.

    Can you share this script? This may help with suggestions...

    Can someone come up with an algorithm that can count these and output the results like I have listed?

    Sounds like a hash--where the keys store the error--may serve your purpose.

      Thank you for the reply

      There is really no way for me to post my entire script as it is too long and on my unix box and it cannot be connected to the internet...

      I will post some of the script here that I've been working on

      my $count = 0; $array_length = grep m/^.*\n/, @storage; #Storage array contains my fi +le.. Use grep to get the number of sentences for (my $i = 0; $i <= $array_length; $i++) { my $x = 1; if ($storage[$i] eq $storage[$x]) { $count++; } $x++; }

      That will return the number of matches of the first 12 sentences in the file... until it hits a line that is not like the first 12...

      I do not know how to make it gather a count of all of the lines that are the same and return a count for each of them

        Hi, gather a count of all of the lines that are the same
        You may have to check each of the lines, because not all the lines are the same. e.g

        User logged in successfully at 10:35:33 User logged in successfully at 10:35:34 User logged in successfully at 10:35:35 User logged in successfully at 10:35:37
        The lines above are not the same, because of the changing time the user logged in.

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
Re: Counting the number of times several strings appear
by rpnoble419 (Pilgrim) on Nov 13, 2012 at 17:22 UTC
    I would read the file in to a database. Then do distinct counts on each entry. This gives you the ability to do long term tracking as well as spot tracking to better refine you process or track errors
Re: Counting the number of times several strings appear
by thundergnat (Deacon) on Nov 13, 2012 at 17:52 UTC

    Maybe you can adapt something like this...

    use warnings; use strict; my %log; while (my $line = <DATA>){ chomp $line; next unless length $line; my ($event, $time) = split / at /, $line; push @{$log{$event}}, $time; } for my $key (keys %log){ print $key, ${$log{$key}}[0] ? " at ${$log{$key}}[0]" : ''; print "\nThe above event occurred ", $#{$log{$key}}, " additional +times.\n"; } __DATA__ User logged in successfully at 10:35:33 User logged in successfully at 10:35:34 User logged in successfully at 10:35:35 Error: Login failure! User logged in successfully at 10:35:37 Error: Login failure! Error: System failure at 01:23:22 Error: System failure at 01:23:40 User logged in successfully at 10:35:39 Error: System failure at 01:23:41

    Yields: (on my system, output order may vary.)

    Error: Login failure!
    The above event occurred 1 additional times.
    Error: System failure at 01:23:22
    The above event occurred 2 additional times.
    User logged in successfully at 10:35:33
    The above event occurred 4 additional times.
    

      Thank you, that seems to be exactly what I needed

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1003664]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2014-12-20 13:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls