awk合并来自多个文件的列,附加不同的值并删除相同的值

时间:2014-12-04 17:35:44

标签: awk merge duplicates

我有两个文件:

try7.txt
a   32145
b   eioue
c   32654895
d   bdefgac
e   kkloi
f   6549465
g   test123452
h   est0124358

try8.txt
a   32145562
b   eioueddf
c   32654
d   bdefgac
e   kkloi
f   6549465dww
g   test123
h   est0124358df
i   63574968fd
j   dfsdfcd5

期望的输出:

a  32145562      32145
b  eioueddf      eioue
c  32654         32654895
d  bdefgac       0
e  kkloi         0
f  6549465dww    6549465
g  test123       test123452
h  est0124358df  est0124358
i  63574968fd    0
j  dfsdfcd5      0

实际输出:

a  32145562      32145
b  eioueddf      eioue
c  32654         32654895
d  bdefgac       bdefgac
e  kkloi         kkloi
f  6549465dww    6549465
g  test123       test123452
h  est0124358df  est0124358
i  63574968fd    0
j  dfsdfcd5      0

我找到的代码:

awk 'NR==FNR{a[$1]=$2;next}
    {if($1 in a){print $0,a[$1];delete a[$1]}
        else print $0,"0"}
    END{for(x in a)print x,"0",a[x]}' try7.txt try8.txt|sort -n|column -t

如何修改这些代码以满足我的要求?

1 个答案:

答案 0 :(得分:0)

比特冗长

awk 'FNR==NR{a[$1]=$2; next} 
     ($1 in a) && a[$1] != $2{print $1,$2,a[$1]} 
     ($1 in a) && a[$1] == $2 {print $1, $2,"0"} 
     !($1 in a ){print $1, $2, 0}'  try7 try8

将输出

a  32145562 32145
b eioueddf eioue
c 32654 32654895
d bdefgac 0
e kkloi 0
f 6549465dww 6549465
g test123 test123452
h est0124358df est0124358
i 63574968fd 0
j dfsdfcd5 0