Find the occurence of particular string in log file

I have a log file which looks like this:

[2012-12-04 13:54:15,744] <845185415165:STATUS:5/0:0:0:0:0|ghy59DI5zasldf87asdfamas8df9asd903tGUVSQx4GJVSQ==>

I have to extract DATE and number of times the keyword STATUS is shown on each date.

Input is :

[2012-12-04 13:54:15,744] <1354625655744:STATUS:5/0:0:0:0:0|ghy59DI5ztGUVSQx4GJVSQ==>

Output: in excel file, Column1(date) Column2(Number of occurences)
My excel file should also contain all the dates, if on any particular day, STATUS keyword is not found, it will show count as 0

Please help me ASAP. I am in very urgent need of this shell script.

perl -e 'while(<>){ 
  $counts{$1} = 0 if (/\[(\d{4}-\d{2}-\d{2})/ ) && (! defined $counts{$1})
  $counts{$1}++ if /\[(\d{4}-\d{2}-\d{2}).+STATUS/;
}
for $date (sort keys %counts){
  print "$date , $counts{$date}\n";
} '  filename.log >status_count.csv

thanks for the very quick reply..

But, can you please help me in writing in normal bash script for the same

Why do you need bash specifically perl can be called within a bash script, it's just another utility, a very powerful one that you can use to write complex programs in, but also a command line utility.

skrynesaver...

how do i call a bash script from the perl??

Perl allows you to call a shell command in three ways, exec, system and with backticks or using the qx(), quote executable, operator

exec ("$ENV{HOME}/bin/my_script.sh --f - l -a - g -s");
system ("$ENV{HOME}/bin/my_script.sh -f - l -a - g -s");
$result = qx($ENV{HOME}/bin/my_script.sh -f - l -a - g -s);

exec hands control over to the called operation and the entire process dies with the called script, whereas system forks a new process and waits for it to complete. The qx operator is like system except it returns the output of the command which allows us to assign it.

1 Like

Actually, I have no idea how to write the script. This is actually my first time.
I wrote the code like this:

#!/usr/local/bin/perl

MAIN:
{

        while(<>)
        {
                $counts{$1} = 0 if (/\[(\d{4}-\d{2}-\d{2})/ ) && (! defined $cou
nts{$1})
                $counts{$1}++ if /\[(\d{4}-\d{2}-\d{2}).+STATUS/;
        }

        for $date (sort keys %counts)
        {
                print "$date , $counts{$date}\n";
        }
}

On running the code, I am getting following error:

bash-3.00$ perl test.pl license.log > status.log
Scalar found where operator expected at test.pl line 9, near ")
                $counts"
        (Missing operator before $counts?)
syntax error at test.pl line 9, near ")
                $counts"
syntax error at test.pl line 9, near "++ if"
syntax error at test.pl line 16, near "}"
Execution of test.pl aborted due to compilation errors.

Can you help me in this?

#!/usr/local/bin/perl

use strict;
use warnings;

my %counts;
while(<>){
    $counts{$1} = 0 if (/\[(\d{4}-\d{2}-\d{2})/ ) && (! defined $counts{$1});
    $counts{$1}++ if /\[(\d{4}-\d{2}-\d{2}).+STATUS/;
}
for my $date (sort keys %counts){
    print "$date , $counts{$date}\n";
}

I missed a semi-colon :((

This is a way to do it in awk:

awk -F'[[ ]' '{d[$2]}/STATUS/{d[$2]++}END{for(i in d)print i,d?d:0}' OFS=, filename