Hi Linux Gurus !
My input looks like:
Code:
first_A|last_B|bio|score_100
first_A|last_B|math|score_50
first_C|last_D|bio|score_70
first_C|last_D|phys|score_35
first_C|last_D|math|score_55
I would need to append on a same line all the information related to the same first and second fields:
Code:
first_A|last_B|bio|score_100|math|score_50
first_C|last_D|bio|score_70|phys|score_35|math|score_55
For that I tried to use the ternary ( ?: ) as follow:
Code:
BEGIN{FS=OFS="|"}
{
array[$1$2] = (array[$1$2] ? array[$1$2] : $1 FS $2) FS $3 FS $4
print array[$1$2]
}
But I get all the intermediate steps until I reach the output I want:
Code:
first_A|last_B|bio|score_100
first_A|last_B|bio|score_100|math|score_50
first_C|last_D|bio|score_70
first_C|last_D|bio|score_70|phys|score_35
first_C|last_D|bio|score_70|phys|score_35|math|score_55
I tried to empty the variable first but it cannot save the previous field and I get the same as input:
Code:
BEGIN{FS=OFS="|"}
{
f1 = ""
f1 = (array[$1$2] ? array[$1$2] : $1 FS $2) FS $3 FS $4
$0 = f1
print $0
}
Does someone see what I am doing wrong?