This HOW TO is more for my notes. We had a request at work, where we had to parse some log files and create a graph from the data in the log files.
The log files looked like this
[bash]
0m0.107s
0m0.022s
0m0.015s
2011-01-05_02_22
0m0.102s
0m0.024s
0m0.014s
2011-01-05_02_23
[/bash]
I wrote the following perl script to get the log file to look as such
[bash]| 0m0.107s| 0m0.022s| 0m0.015s| 2011-01-05 | 02:22
| 0m0.102s| 0m0.024s| 0m0.014s| 2011-01-05 | 02:23 [/bash]
perl script
[perl]
#!/usr/bin/perl
# Modules to load
# use strict;
use warnings;
# Variables
my $inputFile = ‘input.txt’;
my $version = 0.1;
my $logFile = ‘parsed_input.csv’;
# Sub Functions
sub Log($$$);
sub Trim($);
# Clear the screen
system $^O eq ‘MSWin32’ ? ‘cls’ : ‘clear’;
# Open the output log file
open(LOGFILE,"> $logFile") || die "Couldn’t open $logFile, exiting $!\n";
# Open the input file
open(INPUTFILE,"< $inputFile") || die "Couldn’t open $inputFile, exiting $!\n";
# Process the input file, one line at a time
while (defined ($line = <INPUTFILE>)) {
chomp $line;
# Check for blank line
if ($line =~ /^$/)
{
# Start a new line in the output
print LOGFILE "\n";
}
else
{
# Split the date and time
if ($line =~ /2011/)
{
@date = split (/_/,$line);
print LOGFILE "| $date[0] | $date[1]:$date[2]";
}
else
{
# Write the value to the output
print LOGFILE "| $line";
}
}
}
[/perl]
I then took the parsed log files and imported them into the cloud based reporting engine provided by Zoho at http://reports.zoho.com
The final result are these reports
Did I say, I love technology? 🙂