awk将ChildIDs列转换为Row

时间:2014-11-26 17:43:02

标签: awk

想要转换ChildID -2nd字段(即对着Desc / ParentID:row的列到行)

Tr_Input.txt

Desc,ParentID,ChildID,Location
mmmm,aa123aa,11,www
pppp,bb456bb,40,www
mmmm,aa123aa,33,www
pppp,bb456bb,20,www
xxxx,cc789cc,88,www
mmmm,aa123aa,22,www
pppp,bb456bb,60,www
mmmm,aa123aa,44,www
xxxx,cc789cc,88,www

当我们考虑ParentID时,以下命令正常工作,ChildID是唯一的:

awk 'BEGIN { FS=OFS=","} {a[$2]=a[$2] FS $3} END {for (i in a) {print i,a[i]} }' Tr_Input.txt

输出:

ParentID,ChildID's 
,,
aa123aa,,11,33,22,44
cc789cc,,88,88
bb456bb,,40,20,60

在尝试添加一个像“Desc,ParentID”明智的ChildID这样的字段时,得到了打击,尝试了以下命令并取消了成功:

awk 'BEGIN { FS=OFS=","} {a[$1]=a[$1] FS a[$2] FS $3} END {for (i in a) {print i,a[i]} }' Tr_Input.txt

输出

,,,
aa123aa,
cc789cc,
mmmm,,,11,,33,,22,,44
pppp,,,40,,20,,60
xxxx,,,88,,88
bb456bb,

期望的输出:

Desc,ParentID,ChildID's
mmmm,aa123aa,11,33,22,44
xxxx,cc789cc,88,88
pppp,bb456bb,40,20,60

寻找你的建议!!!

1 个答案:

答案 0 :(得分:1)

awk -F, -v OFS=, '
    NR > 1 {desc[$2]=$1; child[$2] = child[$2] OFS $3} 
    END {
        print "Desc,ParentID,ChildID'\''s"
        for (parent in desc)
            print desc[parent] OFS parent child[parent]
    }
' filename
Desc,ParentID,ChildID's
mmmm,aa123aa,11,33,22,44
xxxx,cc789cc,88,88
pppp,bb456bb,40,20,60

"孩子" array是实际累积childID的地方。我很想在perl中编写代码,因为数据结构更灵活,但你想要awk。