2016-07-11 235 views
0

我有两个基于一列公用列进行合并的制表符分隔列。 例如:基于一个公共列合并两个多列文件

文件1:

abandoning 0 V 
abandonment 0 N 
abandonments 0 N 
abandons 0 V 
abducted 0 V 
abduction 0 N 

文件2:

abandonment 
abducted 
abduction 
abound 
abounds 
abundance 
abundant 
accessable 

我想这些文件合并到具有如果该信息为空值,第三个文件不可用。

文件3(期望的结果):

abandoning 0 V 
abandonment 0 N 
abandonments 0 N 
abandons 0 V 
abducted 0 V 
abduction 0 N 
abound 
abounds 
abundance 
abundant 
accessable 

我一直在四处寻找hereherehere。 到目前为止,我所看到的最接近的事是这样的:

awk '{a[$1]=a[$1] FS $2} END {for (i in a) print i a[i]}' OrigFile.txt ToMerge.txt | sort > Merged_Dict.txt 

然而,结果不包括第三列信息。 ,我得到的结果是:

abandoning 0 
abandonment 0 
abandonments 0 
abandons 0 
abducted 0 
abduction 0 
abound 
abounds 
abundance 
abundant 
accessable 

任何提示,以我要去的地方错了吗?

+0

检查这个..http:/ /stackoverflow.com/questions/31401328/search-and-merge-multiple-files-in-unix/31402354#31402354 –

回答

1

你,可以做,这是awk但对于这个工具已如果你的文件已经排序

$ join -a1 -a2 file1 file2 

abandoning 0 V 
abandonment 0 N 
abandonments 0 N 
abandons 0 V 
abducted 0 V 
abduction 0 N 
abound 
abounds 
abundance 
abundant 
accessable 

这里是一个awk解决

$ awk 'NR==FNR{a[$0];next} $1 in a{delete a[$1]}1; END{for(k in a) print k}' file2 file1 | 
    sort 
相关问题