1)合并 file1, file2 的公共部分
join file1.txt file2.txt >file3.txt
结果在 file3.txt 里:
1 2 4
4 5 8
2)分别分析 file1, file2 与 file3 的不同部分:
join -v1 file1.txt file3.txt >tmp1
join -v1 file2.txt file3.txt >tmp2
tmp1里的内容就是:2 3
tmp2里的内容就是:7 9
3)分别对tmp1, tmp2的内容进行补0并添加到file3.txt中:
awk '{print $0" "0}' tmp1 >>file3.txt
awk '{print $1” "0" " $2}' tmp2 >>file3.txt
4)最后对 file3.txt 排序即可:
sort file3.txt -o output.txt
你要的结果就在output.txt里。
--------------------------------------------------------------------------
给个完整的脚本吧:
#!/bin/sh
join file1.txt file2.txt >file3.txt
join -v1 file1.txt file3.txt >tmp1
join -v1 file2.txt file3.txt >tmp2
awk '{print $0" "0}' tmp1 >>file3.txt
awk '{print $1” "0" " $2}' tmp2 >>file3.txt
sort file3.txt -o output.txt
rm tmp1 tmp2 file3.txt
exit 0