Chan Kim
Chan Kim

Reputation: 5959

awk string concatenation not working

I have this file

*PADS2000*
*PART*
C1               C2012
C10              C2012

*NET*
*SIGNAL* B2B_12V
F1.2 TC1.1
CNB2.88 U2.1
*SIGNAL* DDR3_VREF1
U9.M8 U9.H1
U5.6 C24.1
*END*

and I want to convert it to look like this (list signal name and all the connected part.pin names in a single line).

*SIGNAL* B2B_12V F1.2 TC1.1 CNB2.88 U2.1
*SIGNAL* DDR3_VREF1 U9.M8 U9.H1 U5.6 C24.1

The awk script I wrote is like this.

BEGIN{print "========================="}
(d || f ) && (/*SIGNAL*/ || /*END/) {print "HAHAHA : " xline}
/*SIGNAL*/{kept=$0; f = 1; xline=$0; next}
f {print kept; f = 0; d = 1; print; xline=xline " " $0; next}
d {print; xline=xline " " $0}

The output is like this.

=========================
*SIGNAL* B2B_12V
F1.2 TC1.1 
CNB2.88 U2.1 
 CNB2.88 U2.1 AL* B2B_12V
*SIGNAL* DDR3_VREF1
U9.M8 U9.H1 
U5.6 C24.1 
 U5.6 C24.1  NAL* DDR3_VREF1
*END*

What is wrong? (It looks like the strings are overwritten every time when it should be concatenated.)

EDIT : I later found that this works as I wanted in Cygwin. my CentOS shell seems to have something making it weird. If I run it on Cygwin, it runs like below as I expected. (I'll come to my shell problem when I have time.)

$ awk -f why.awk in.dat
=========================
*SIGNAL* B2B_12V
U24.2 TC1.1
CNB2.88 U2.1
HAHAHA : *SIGNAL* B2B_12V U24.2 TC1.1 CNB2.88 U2.1
*SIGNAL* DDR3_VREF1
U9.M8 U24.H1
U5.6 C24.1
HAHAHA : *SIGNAL* DDR3_VREF1 U9.M8 U24.H1 U5.6 C24.1
*END*

Upvotes: 2

Views: 211

Answers (1)

Chan Kim
Chan Kim

Reputation: 5959

I dit it using gawk on Cygwin but it also works on my CentOS 6.7 shell. But there is some problem with my CentOS shell as I added on the EDIT above. For anyone's benefit, I attach my gawk script here :

BEGIN{print "========================="}

(f || d ) && (/*SIGNAL*/ || /*END*/) {
f=0; d = 0; n = asort(xline);
printf "%s ", kept;
for (i=1;i<=ix; i++){
printf "%s ", xline[i];
}
printf "\n";
ix = 0;
}

/*SIGNAL*/{
kept = $0; f = 1; next}

f {
f = 0; d = 1;
for(i=1;i<=NF;i++) {
    xline[ix] = $i;
    ix = ix + 1;
}
next}

d {
for(i=1;i<=NF;i++) {
    xline[ix] = $i;
    ix = ix + 1;
}
next}

Output :

*SIGNAL* B2B_12V CNB2.88 TC1.1 U2.1 U24.2
*SIGNAL* DDR3_VREF1 C24.1 U24.2 U24.H1 U5.6

EDIT :
I later found that this strange phenomenon was because the input text file was from DOS(Windos). See Adam Katz's answer in awk string concatenation not working 2 (shell setting error?).

Upvotes: 1

Related Questions