2 个文件之间的 vlookup 函数并在 EOL 处追加匹配项

vlookup function between 2 files and append matches at EOL

需要从具有多个条目的两个不同文件进行 vlookup:

猫file1.csv

aaaaaaa;24/09/2018;06/09/2018;1;89876768
bbbbbbb;15/09/2018;03/09/2018;2;76958489
ccccccc;10/09/2018;28/08/2018;3;57848472
ddddddd;22/09/2018;08/09/2018;4;17929730
eeeeeee;19/09/2018;30/08/2018;5;18393770

猫file2.csv

20180901;abc;1
20180901;sdf;2
20180904;jhh;2
20180905;skf;3
20180911;asf;2
20180923;ghf;4
20180925;asb;4
20180918;mnj;3

另外对于file1.csv,第四列是第三列的标识符进入file2.csv.

所需的输出是:

aaaaaaa;24/09/2018;06/09/18;1;89876768;20180901
bbbbbbb;15/09/2018;03/09/18;2;76958489;20180901;20180904;20180911
ccccccc;10/09/2018;28/08/18;3;57848472;20180905;20180918
ddddddd;22/09/2018;08/09/18;4;17929730;20180923;20180925
eeeeeee;19/09/2018;30/08/18;5;18393770;unknown

能否请您尝试以下。

awk 'BEGIN{FS=OFS=";"}FNR==NR{a[$NF]=a[$NF]?a[$NF] OFS :;next} {print ( in a)?[=10=] OFS a[]:[=10=] OFS "unknown"}' file2.csv  file1.csv

输出如下。

aaaaaaa;24/09/2018;06/09/2018;1;89876768;20180901
bbbbbbb;15/09/2018;03/09/2018;2;76958489;20180901;20180904;20180911
ccccccc;10/09/2018;28/08/2018;3;57848472;20180905;20180918
ddddddd;22/09/2018;08/09/2018;4;17929730;20180923;20180925
eeeeeee;19/09/2018;30/08/2018;5;18393770;unknown

代码解释:

awk '
BEGIN{                                              ##Starting BEGIN section for awk here.
  FS=OFS=";"                                        ##Setting values for FS and OFS as semi colon.
}                                                   ##Closing block for BEGIN section here.
FNR==NR{                                            ##Checking condition FNR==NR which will be TRUE when first Input_file named file2.csv is being read.
  a[$NF]=a[$NF]?a[$NF] OFS :                    ##Creating an array named a whose index is $NF and value is  and concatenating its own value with same index.
  next                                              ##next will skip all further statements from here.
}                                                   ##Closing block for FNR==NR condition here.
{
  print ( in a)?[=12=] OFS a[]:[=12=] OFS "unknown"     ##These statements will execute when 2nd Input_file is being read and printing value of [=12=] with condition if  is present in array a then concatenate its value with current line else concatenate unknown with it.
}' file2.csv  file1.csv                             ##Mentioning Input_file names here.