在AWK中合并两行

在AWK中合并两行,awk,Awk,我的数据格式如下: Ind1 0 1 2 Ind1 0 2 1 Ind2 1 1 0 Ind2 2 2 0 我想使用AWK获得以下输出: Ind1 00 12 21 Ind2 12 12 00 即合并具有相同行名称的每两行 非常感谢您的帮助。文件a.awk: { col2[$1] = col2[$1] $2 col3[$1] = col3[$1] $3 col4[$1] = col4[$1] $4 } END { for ( i in col2 )

我的数据格式如下:

Ind1 0 1 2
Ind1 0 2 1
Ind2 1 1 0 
Ind2 2 2 0
我想使用AWK获得以下输出:

Ind1 00 12 21
Ind2 12 12 00 
即合并具有相同行名称的每两行

非常感谢您的帮助。

文件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]
    }
}
运行:


cat data | awk-f a.awk

此版本仅假设所有应分组的行都是连续的

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()
    }
'
awk'
函数输出(){
打印文件“%s”,上一页

for(i=2;i?非常感谢,但我的问题是我的真实文件有1000多行和大约400000列,所以我不能使用此代码。