2

我有一个这样的空格分隔文件:

GID_1 AID_1 2
GID_1 AID_2 1
GID_1 AID_3 3
GID_2 AID_1 3
GID_2 AID_2 1
GID_2 AID_3 2
GID_3 AID_1 1
GID_3 AID_2 1
GID_3 AID_3 3

我想将第 1 列和第 3 列转置为第 2 列,并得到这个:

(X)   GID_1 GID_2 GID_3
AID_1  2     3     1
AID_2  1     1     1
AID_3  3     2     3

我放了一个“(X)”,因为这个换位会使该字段变为空白,这对我来说很好。

请问,有人可以帮我吗?

4

1 回答 1

1

An awk solution

awk -F" |_" '
    {
    g=g>$2?g:$2
    a=a>$4?a:$4
    arr[$2 " " $4]=$5
    } 
    END {
    for (j=1;j<=g;j++)
        {
        printf "\tGID_%s",j
        }
    print ""
    for (i=1;i<=a;i++)
        {
        printf "AID_%s",i
        for (j=1;j<=g;j++)
            {
            printf "\t%s",arr[j " " i]
            }
        print ""
        }
    }
    ' file
        GID_1   GID_2   GID_3
AID_1   2       3       1
AID_2   1       1       1
AID_3   3       2       3
于 2013-10-23T08:38:42.633 回答