文件一:
123, 234
345, 789
678, 900
文件2
123, abc
345, dnc
所需的输出(文件 1 中的所有列和文件 2 中的缺失值均为 NA):
123,234,abc
345,789,dnc
678,900,NA
答案1
如果你使用的话会更容易加入:
$ join -t, -a 1 -a 2 -j 1 -e ' NA' -o auto file1 file2
123, 234, abc
345, 789, dnc
678, 900, NA
答案2
awk -F ", ?" -v OFS=, 'FNR == NR { a[$1]=$2;next; };
{ if ($1 in a) print $1,$2,a[$1]; else print $1,$2,"NA"; }' file2 file1
答案3
awk
awk 'BEGIN{OFS=""}NR==FNR{a[$1]=$2;};NR>FNR{if($1 in a){print $0,", ",a[$1];}else{print $0,", NA";}}' file2 file1 > tmp
输出将存储在 tmp 中,正如您要求从 file3 更新一样
awk 'BEGIN{OFS=""}NR==FNR{a[$1]=$2;};NR>FNR{if($1 in a){print $0,", ",a[$1];}else{print $0,", NA";}}' file3 tmp
输出:
123, 234, abc, 2014/10/20
345, 789, dnc, NA
678, 900, NA, 2013/02/30