用perl / awk加入断线

时间:2013-04-23 08:41:37

标签: perl unix join sed awk

我有一个包含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语句的结尾必须是(.*);$

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

5 个答案:

答案 0 :(得分:1)

试试这个单行:

awk '!/;$/{printf "%s",$0}/;$/{print}' file

答案 1 :(得分:1)

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

tr '\n' ' ' < file | sed 's/;/;\n/g'

答案 2 :(得分: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;

答案 3 :(得分:0)

Perl解决方案:

perl -ne 'chomp $last unless /^PP/; print $last; $last = $_ }{ print $last' FILE.SQL

答案 4 :(得分:0)

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

awk '
    /^PP/ {insql=1}
    /;$/  {insql=0}
    insql {printf "%s", $0; next}
          {print}
' file