Regex 使用perl,如何将日志中的sql块转换为正确的sql?
我有一个带有sql调试的hugh日志文件。我想将它们更改为正确的sql 例如,在日志中:Regex 使用perl,如何将日志中的sql块转换为正确的sql?,regex,perl,Regex,Perl,我有一个带有sql调试的hugh日志文件。我想将它们更改为正确的sql 例如,在日志中: **** 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
**** 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--]
下面是perl代码
#!/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);
我需要改变上面的陈述如下
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')
有人能告诉我如何才能做到这一点吗?任何指导方针都是非常感谢的
借助于user2676655提到的regex分组
这是完整的代码
#!/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);
#/usr/bin/perl
严格使用;
使用警告;
我的$log;
我的$match=0;
我的$q=“”;
我的$p=“”;
打开(文件1,“dps\u ui\u preview\u view.log”);
而(){
如果(/\+\+SQL[Insert,Update]/../\-\-SQL[Insert,Update]/){
$uz=~s/^.*\t//g;
$log=$log.$\u;
如果($\=~m/\-\-\]/){
($q,$p)=($log=~/\+\+\](.*)参数--(.*)\[-/s);
$p=~s/^\s+/;
my@params=拆分(/\n/,$p);
foreach my$i(@params){
我的($val)=($i=~/\}(.*)\(/);
$val=~s/^\s+/;
$val=~s/\s+$/;
$q=~s/\?/'$val'/;
}
$q=~s/\n//g;
$q=~s/\s+//g;
$q=~s/$/;/g;
打印$q,“\n”;
$log=”“;
}
}
}
关闭(文件1);
试试这个:
$log = "[++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--]";
($q,$p) = ($log =~/\+\+\](.*)-- Parameters --(.*)\[--/s);
$p =~ s/^\s+//;
@params = split(/\n/, $p);
foreach (@params) {
my ($val) = ($_ =~/\}(.*)\(/) ;
$val =~ s/^\s+//;
$val =~ s/\s+$//;
$q =~s/\?/'$val'/;
}
$q =~s/\n/ /g;
$q =~s/\s+/ /g;
print $q;
您试图插入的路径是否也在日志文件中?从日志文件中,我可以得到[++sql*]..[--sql*]将有更新和插入语句,参数值可以是路径。非常感谢您的回答,我正在学习perl,我还没有想到正则表达式分组。
$log = "[++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--]";
($q,$p) = ($log =~/\+\+\](.*)-- Parameters --(.*)\[--/s);
$p =~ s/^\s+//;
@params = split(/\n/, $p);
foreach (@params) {
my ($val) = ($_ =~/\}(.*)\(/) ;
$val =~ s/^\s+//;
$val =~ s/\s+$//;
$q =~s/\?/'$val'/;
}
$q =~s/\n/ /g;
$q =~s/\s+/ /g;
print $q;