Given a large input file that looks like this:
02/26/2012 08:54:38 Error:java.sql.Exception
02/26/2012 08:54:48 Error:java.sql.Exception
02/26/2012 08:56:05 Error:java.sql.Exception
02/26/2012 08:57:21 Error:java.sql.Exception
02/26/2012 08:59:29 Error:java.sql.Exception
02/26/2012 09:01:14 Error:java.sql.Exception
02/26/2012 09:08:48 Error:java.sql.Exception
02/26/2012 09:10:41 Error:java.sql.Exception
I am trying to find out the count of errors per hour; that is, I am looking for an output file that looks like this:
02/26/2012 08 -> 5
02/26/2012 09 -> 3
Here is a script that is working for me:
#!/bin/perl
open(MYFILE, 'tata2');
my %table;
while (<MYFILE>) {
chomp;
$dtkey = substr $_, 0, 13;
$table{$dtkey}++;
}
close(MYFILE);
for my $key (keys %table) {
print "$key -> $table{$key}\n";
}
But given Perl’s features, I am pretty sure this can be done in fewer lines.
I’d greatly appreciate if you can provide some examples. I hope it will be useful for those who want to reduce lines of code written to achieve something.
What you have is already fairly short. You can improve things a bit by using lexical file handles and checking the return value of open.
Here is a rewrite using some of Perl’s other syntactic features:
Or if you really want it short, how about a one liner: