سؤال

I have a file with the below format. This is a snippet, original lines cross over a million.

Infact ABC/DEF/GH9j/etc.. is the starting of a line and ";" is supposed to be the end of the line.

Here in this example line01, line10 & line13 are perfect; But other lines are scattered in to multiple lines:

line01: ABC abc_123 ( .Y ( B2b ) , .A ( sel ) );

line02: DEF def_456 ( .Z ( n_2 ) , .in ( 1b0 ) ,

line03: .tstin ( sel ) , .tstmb ( DD ) );

line04: GH9j 3_inst ( .Q0 ( CC3 ) , .Q1 ( Ee ) ,

line05: .Q2 ( p_2 ) , .Q3 ( cin ) ,

line06: .D0 ( AA ) , .D1 ( rdata[5] ) ,

line07: .D2 ( gg ) , .D3 ( hp ) ,

line08: .SE0 ( sel ) , .SE1 ( sel ) ,

line09: .SE2 ( pqr ) , .SE3 ( AA ) , .CK ( Bb ) );

line10: BUF 4PQR ( .Y ( eE ) , .A ( cC ) );

line11: MX2 MnOp ( .X ( DD ) , .A ( PQR_11 ) ,

line12: .B ( trstb ) , .S0 ( klm2 ) );

line13: BUFH 6th_inst ( .Zz ( AA ) , .A ( B2B ) );

.......

Q. I want to rearrange all lines like below: All the ports of one instance should be in ONE LINE (13 lines should reduce to 6 lines)

line01: ABC abc_123 ( .Y ( B2b ) , .A ( sel ) );

line02: DEF def_456 ( .Z ( n_2 ) , .in ( 1b0 ) , .tstin ( sel ) , .tstmb ( DD ) );

line03: GH9j 3_inst ( .Q0 ( CC3 ) , .Q1 ( Ee ) , .Q2 ( p_2 ) , .Q3 ( cin ) , .D0 ( AA ) , .D1 ( rdata[5] ) , .D2 ( gg ) , .D3 ( hp ) , .SE0 ( sel ) , .SE1 ( sel ) , .SE2 ( pqr ) , .SE3 ( AA ) , .CK ( Bb ) );

line04: BUF 4PQR ( .Y ( eE ) , .A ( cC ) );

line05: MX2 MnOp ( .X ( DD ) , .A ( PQR_11 ) , .B ( trstb ) , .S0 ( klm2 ) );

line06: BUFH 6th_inst ( .Zz ( AA ) , .A ( B2B ) );

هل كانت مفيدة؟

المحلول

A one liner might work: perl -pe' chomp unless m/\;/' file.txt - though with millions of lines some tweaking might be needed.

Here is the one liner as a script:

#!/usr/bin/env perl

while (<DATA>) {
    chomp unless m/\;/ ;
    print ;
}

__DATA__
ABC abc_123 ( .Y ( B2b ) , .A ( sel ) );
DEF def_456 ( .Z ( n_2 ) , .in ( 1b0 ) ,
.tstin ( sel ) , .tstmb ( DD ) );
GH9j 3_inst ( .Q0 ( CC3 ) , .Q1 ( Ee ) ,
.Q2 ( p_2 ) , .Q3 ( cin ) ,
.D0 ( AA ) , .D1 ( rdata[5] ) ,
.D2 ( gg ) , .D3 ( hp ) ,
.SE0 ( sel ) , .SE1 ( sel ) ,
.SE2 ( pqr ) , .SE3 ( AA ) , .CK ( Bb ) );
BUF 4PQR ( .Y ( eE ) , .A ( cC ) );
MX2 MnOp ( .X ( DD ) , .A ( PQR_11 ) , 
.B ( trstb ) , .S0 ( klm2 ) );
BUFH 6th_inst ( .Zz ( AA ) , .A ( B2B ) );

Output:

ABC abc_123 ( .Y ( B2b ) , .A ( sel ) );
DEF def_456 ( .Z ( n_2 ) , .in ( 1b0 ) ,.tstin ( sel ) , .tstmb ( DD ) );
GH9j 3_inst ( .Q0 ( CC3 ) , .Q1 ( Ee ) ,.Q2 ( p_2 ) , .Q3 ( cin ) ,.D0 ( AA ) , .D1 (  rdata[5] ) ,.D2 ( gg ) , .D3 ( hp ) ,.SE0 ( sel ) , .SE1 ( sel ) ,.SE2 ( pqr ) , .SE3 ( AA ) , .CK ( Bb ) );
BUF 4PQR ( .Y ( eE ) , .A ( cC ) );
MX2 MnOp ( .X ( DD ) , .A ( PQR_11 ) ,.B ( trstb ) , .S0 ( klm2 ) );
BUFH 6th_inst ( .Zz ( AA ) , .A ( B2B ) );

If you need to preserve the line numbers (line01:) add a note to that effect in the question.

نصائح أخرى

Here is a sed solution:

sed -n 'H;/;/{s/.*//;x;s/\n//g;p;}' filename

Without seeing your whole dataset, or at least large chuncks of it, its hard to know what pattern to match on, however in the example you give, it seems as though the lines you want to merge end in a ,newline therefore simply changing ,newline to , might do the trick:

sed -e ':begin;$!N;s/,\n/,/;tbegin;P;D' in.txt

outputs:

ABC abc_123 ( .Y ( B2b) , .A ( sel));
DEF def_456 ( .Z ( n_2) , .in ( 1b0) ,.tstin ( sel) , .tstmb ( DD));
GH9j 3_inst ( .Q0 ( CC3) , .Q1 ( Ee) ,.Q2 ( p_2) , .Q3 ( cin) ,.D0 ( AA) , .D1 ( rdata [5]) ,.D2 ( gg) , .D3 ( hp) ,.SE0 ( sel) , .SE1 ( sel) ,.SE2 ( pqr) , .SE3 ( AA) , .CK ( Bb));
BUF 4PQR ( .Y ( eE) , .A ( cC));
MX2 MnOp ( .X ( DD) , .A ( PQR_11) ,.B ( trstb) , .S0 ( klm2));
BUFH 6th_inst ( .Zz ( AA) , .A ( B2B));

See http://backreference.org/2009/12/23/how-to-match-newlines-in-sed/ for more details on matching newlines in sed.

A one-liner in AWK

awk '{printf "%s%s", $0, /;\s*$/ ? "\n" : " "}' filename

It outputs each line with a separator that is either a newline or space depending on whether ; is found at the end of the line.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top