#!/usr/bin/perl # Munge MySQL logfiles to extract just the queries. use strict; use warnings; my @infile = <>; my @entries = breakQueryLogFileIntoEntries(@infile); @entries = joinLinesFromEachEntry(@entries); @entries = extractQueryPart(@entries); #print join "\n-----------------------------------------\n", @entries; print join "\n", @entries; # break a query log file into an array of query entries # input: array of log-file lines # output: array of log-file entries sub breakQueryLogFileIntoEntries { my @inFile = @_; my $thisLogEntry = ''; my @allLogEntries = (); my $new_log_entry_re = qr/^\s+\d+|^\d{6}/; # beginning of a new log entry (possibly multiline) foreach my $line ( @inFile ) { if( $line =~ m{$new_log_entry_re} ) { push @allLogEntries, $thisLogEntry; # close out the previous entry $thisLogEntry = $line; #...and start a new one } else { $thisLogEntry .= $line; } } return @allLogEntries; } sub joinLinesFromEachEntry { my @array = @_; foreach(@array) { s/\n|\r\n//gsm; # is this the most portable way to do it? } return @array; } # MySQL logs display some junk before the actual query. Extract just # the query part sub extractQueryPart { my @inArray = @_; my @outArray = (); my $query_line_re = qr/^\s+\d+\s+Query\s+/; foreach(@inArray) { if( m/$query_line_re/ ) { s/$query_line_re//; push @outArray, $_; } else { next; } } return @outArray; }