I have a hugh log file with sql debugs. I want to change them to proper sql.
For example in logs:
**** debug Fri Aug 09 13:05:13 PDT 2013 1376078713845 /atg/web/viewmapping/ViewMappingRepository executeUpdate2: [++SQLInsert++]
**** debug Fri Aug 09 13:05:13 PDT 2013 1376078713845 /atg/web/viewmapping/ViewMappingRepository INSERT INTO vmap_pv2pvad_rel(view_id,name,attr_id)
**** debug Fri Aug 09 13:05:13 PDT 2013 1376078713845 /atg/web/viewmapping/ViewMappingRepository VALUES(?,?,?)
**** debug Fri Aug 09 13:05:13 PDT 2013 1376078713845 /atg/web/viewmapping/ViewMappingRepository -- Parameters --
**** debug Fri Aug 09 13:05:13 PDT 2013 1376078713845 /atg/web/viewmapping/ViewMappingRepository p[1] = {pd} AmPvPuRoleAllMembers (java.lang.String)
**** debug Fri Aug 09 13:05:13 PDT 2013 1376078713845 /atg/web/viewmapping/ViewMappingRepository p[2] = {pd} synthCategoryDefinition (java.lang.String)
**** debug Fri Aug 09 13:05:13 PDT 2013 1376078713845 /atg/web/viewmapping/ViewMappingRepository p[3] = {pd: attributes} AmPvAttPuRolesMembersCatDef (atg.adapter.gsa.SingleValueGSAId)
**** debug Fri Aug 09 13:05:13 PDT 2013 1376078713845 /atg/web/viewmapping/ViewMappingRepository [--SQLInsert--]
With below perl code
#!/usr/bin/perl
use strict;
use warnings;
open(FILE1, 'dps_ui_preview_view.log');
while (<FILE1>) {
if (/\+\+SQL[Insert,Update]/../\-\-SQL[Insert,Update]/) {
$_ =~ s/^.*\t//g;
print $_;
}
}
close(FILE1);
I am able to grab below sql format
[++SQLUpdate++]
UPDATE vmap_im
SET item_path=?
WHERE id=?
-- Parameters --
p[1] = {pd: itemPath} /atg/userprofiling/ExternalProfileRepository (java.lang.String)
p[2] = {pd} AmImEuUsers (java.lang.String)
[--SQLUpdate--]
[++SQLInsert++]
INSERT INTO vmap_fh(id,name,component_path)
VALUES(?,?,?)
-- Parameters --
p[1] = {pd} AmFhPuFH (java.lang.String)
p[2] = {pd: name} External Preview Users formHandler (java.lang.String)
p[3] = {pd: path} /atg/remote/userprofiling/assetmanager/editor/service/PreviewUserAssetService (java.lang.String)
[--SQLInsert--]
I need to change above statements like below
UPDATE vmap_im SET item_path='/atg/userprofiling/ExternalProfileRepository' WHERE id='AmImEuUsers';
INSERT INTO vmap_fh(id,name,component_path) VALUES('AmFhPuFH','External Preview Users formHandler','/atg/remote/userprofiling/assetmanager/editor/service/PreviewUserAssetService')
Can anyone please let me know how can I achieve that? Any guide lines is much appreciated.
With help of regex grouping mentioned by user2676655 Here is the full code
#!/usr/bin/perl
use strict;
use warnings;
my $log;
my $match = 0;
my $q="";
my $p="";
open(FILE1, 'dps_ui_preview_view.log');
while (<FILE1>) {
if (/\+\+SQL[Insert,Update]/../\-\-SQL[Insert,Update]/) {
$_ =~ s/^.*\t//g;
$log = $log . $_;
if($_ =~ m/\-\-\]/) {
($q,$p) = ($log =~ /\+\+\](.*)-- Parameters --(.*)\[--/s);
$p =~ s/^\s+//;
my @params = split(/\n/,$p);
foreach my $i (@params) {
my ($val) = ($i =~ /\}(.*)\(/);
$val =~ s/^\s+//;
$val =~ s/\s+$//;
$q =~ s/\?/'$val'/;
}
$q =~ s/\n/ /g;
$q =~ s/\s+/ /g;
$q =~ s/ $/;/g;
print $q,"\n";
$log="";
}
}
}
close(FILE1);