Reputation: 911
I have a data format as follows:
Ind1 0 1 2
Ind1 0 2 1
Ind2 1 1 0
Ind2 2 2 0
I want to use AWK to have this output:
Ind1 00 12 21
Ind2 12 12 00
That is to merge each two rows with the same row names.
Thank you very much in advance for your help.
Upvotes: 0
Views: 230
Reputation: 247202
This version only assumes that all the lines that should be grouped are continguous
awk '
function output() {
printf "%s", prev
for (i=2; i<=NF; i++)
printf "%s%s", OFS, col[i]
print ""
}
$1 != prev {
if (NR != 1)
output()
for (i=2; i<=NF; i++)
col[i] = $i
prev = $1
next
}
{
for (i=2; i<=NF; i++)
col[i] = col[i] $i
}
END {
output()
}
'
Upvotes: 0
Reputation: 5685
file a.awk:
{
col2[$1] = col2[$1] $2
col3[$1] = col3[$1] $3
col4[$1] = col4[$1] $4
}
END {
for ( i in col2 )
{
print i " " col2[i] " " col3[i] " " col4[i]
}
}
run:
cat data | awk -f a.awk
Upvotes: 2