awk合并文件一例

群里的朋友求助:

$ cat file1
a 1 2 3
b 2 3 4
c 3 4 5

$ cat file2
d 你
b 好
c 吗

合并两个文件,需要实现:

a 1 2 3
b 2 3 4 好
c 3 4 5 吗
d         你

代码如下:

awk 'NR==FNR{a[$1]=$2;next}{if($1 in a){print $0,a[$1];delete a[$1]}else print}END{for(i in a)print i"	"a[i]}' file2 file1
原文地址:https://www.cnblogs.com/Eivll0m/p/4564921.html