1

我有一个带有 sql 损坏语句的大文件,例如:

PP3697HB @@@@0
<<<<<<Record has been deleted as per PP3697HB>>>>>>
FROM sys.xtab_ref rc,sys.xtab_sys f,sys.domp ur WHE
RE rc.milf = ur.milf  AND rc.molf = f.molf AND ur.dept = 'SWIT'AND ur
.department = 'IND' AND share = '2' AND ur.status = 'DONE' AND f.s
tatus = 'TRUE' AND rc.OPERATOR = '=' AND rc.VALUE = '261366'AND rc.r
unet IN (SELECT milf FROM sys.domp WHERE change = 'OVO' A
ND IND = 75);

我需要将所有这些断线重新组合成一条线。

该行应如下所示:

PP3697HB @@@@0<<<<<<Record has been deleted as per PP3697HB>>>>>>FROM sys.xtab_ref rc,sys.xtab_sys f,sys.domp ur WHERE rc.milf = ur.milf  AND rc.molf = f.molf AND ur.dept = 'SWIT'AND ur.department = 'IND' AND share = '2' AND ur.status = 'DONE' AND f.status = 'TRUE' AND rc.OPERATOR = '=' AND rc.VALUE = '261366'AND rc.runet IN (SELECT milf FROM sys.domp WHERE change = 'OVO' AND IND = 75);

如何在 perl/awk 中实现这一点。

可以说行的开头一定是^PP(.*),sql语句的结尾一定是(.*);$

如果您难以理解问题,请告诉我,我会再次尝试解释。

4

5 回答 5

1

tr用于删除换行符并拆分sed每个 SQL 语句:

tr '\n' ' ' < file | sed 's/;/;\n/g'
于 2013-04-23T08:48:36.270 回答
1

试试这个单行:

awk '!/;$/{printf "%s",$0}/;$/{print}' file
于 2013-04-23T08:47:49.143 回答
0

假设有其他行没有像这样拆分,并且只有指定的行需要重新加入:

awk '
    /^PP/ {insql=1}
    /;$/  {insql=0}
    insql {printf "%s", $0; next}
          {print}
' file
于 2013-04-24T01:42:19.260 回答
0

在 Perl 中尝试这个解决方案:

#!/usr/bin/perl -w    
use strict;
use warnings;
use Data::Dumper;

## The raw string
my $str = "                                                                                                                                                                                                                                  
PP3697HB @@@@0                                                                                                                                                                                                                               
<<<<<<Record has been deleted as per PP3697HB>>>>>>                                                                                                                                                                                          
FROM sys.xtab_ref rc,sys.xtab_sys f,sys.domp ur WHE                                                                                                                                                                                          
RE rc.milf = ur.milf  AND rc.molf = f.molf AND ur.dept = 'SWIT'AND ur                                                                                                                                                                        
.department = 'IND' AND share = '2' AND ur.status = 'DONE' AND f.s                                                                                                                                                                           
tatus = 'TRUE' AND rc.OPERATOR = '=' AND rc.VALUE = '261366'AND rc.r                                                                                                                                                                         
unet IN (SELECT milf FROM sys.domp WHERE change = 'OVO' A                                                                                                                                                                                    
         ND IND = 75);                                                                                                                                                                                                                       
";

## Split the given string as per new line.
my @lines = split(/\n/, $str);

## Join every element of the formed array using blank.
$str = join("", @lines);

print $str;
于 2013-04-23T08:46:04.260 回答
0

Perl 解决方案:

perl -ne 'chomp $last unless /^PP/; print $last; $last = $_ }{ print $last' FILE.SQL
于 2013-04-23T08:47:43.243 回答