contravaluebets
contravaluebets

Reputation: 85

Join broken lines with perl/awk

I have a huge file with sql broken statements like:

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.

We can say that the start of the line must be ^PP(.*) and the end of sql statement must be (.*);$

Let me know if you have difficulty understand the problem and I will try to explain again.

Upvotes: 1

Views: 451

Answers (5)

glenn jackman
glenn jackman

Reputation: 246744

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

Upvotes: 0

Chris Seymour
Chris Seymour

Reputation: 85775

Using tr to remove the newlines and sed to split each SQL statement:

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

Upvotes: 1

Kent
Kent

Reputation: 195029

try this one-liner:

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

Upvotes: 1

choroba
choroba

Reputation: 241768

Perl solution:

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

Upvotes: 0

Krishnachandra Sharma
Krishnachandra Sharma

Reputation: 1342

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

Upvotes: 0

Related Questions