简体   繁体   English

用perl / awk连接虚线

[英]Join broken lines with perl/awk

I have a huge file with sql broken statements like: 我有一个包含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);

I need all these broken lines to be recombined to a single line. 我需要将所有这些虚线重新组合为一行。

The line should look like: 该行应如下所示:

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);

How can I achieve this in perl/awk. 我如何在perl / awk中实现这一目标。

We can say that the start of the line must be ^PP(.*) and the end of sql statement must be (.*);$ 我们可以说行的开头必须是^PP(.*) ,sql语句的结尾必须是(.*);$

Let me know if you have difficulty understand the problem and I will try to explain again. 如果您在理解问题时遇到困难,请告诉我,我将尝试再次解释。

试试这个单线:

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

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

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

Try this solution in Perl: 在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;

Perl解决方案:

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

Assuming there's other lines that are not split up like this, and that only the specified lines require re-joining: 假设还有其他行没有像这样拆分,并且只有指定的行需要重新合并:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM