使用 awk 使用从 file2 到 File1 的所有 4 列对 csv 进行 vlookup

vlookup on csv using all 4 columns from file2 to File1 using awk

尝试对 csv 文件的大型数据集的 4 列进行 vlookup

F1 : 文件 1

TSM,TYPE,NODE,SCHED
AIXTSM1,VHOST,10.199.114.72,DAILY_1800_VM_SDC-CTL-PROD
AIXTSM1,VHOST,ADMET007,DAILY_1800_VM_SDC-CTL-PROD3
AIXTSM2,VHOST,ADMET014,DAILY_1900_VM_UDC-CTL-PROD
AIXTSM1,VHOST,AGGREGATE,DAILY_2200_VM_SDC-CTL-PROD5

F2

AIXTSM1,VHOST,10.199.114.72,DAILY_1800_VM_SDC-CTL-PROD,YES
AIXTSM1,VHOST,ADMET007,DAILY_1800_VM_SDC-CTL-PROD3,NO
AIXTSM2,VHOST,ADMET014,DAILY_1900_VM_UDC-CTL-PROD,YES
AIXTSM1,VHOST,AGGREGATE4,DAILY_2200_VM_SDC-CTL-PROD5,NA

17 日 F1 的预期结果:输入文件 1

TSM,TYPE,NODE,SCHED,2018-11-17
AIXTSM1,VHOST,10.199.114.72,DAILY_1800_VM_SDC-CTL-PROD,YES
AIXTSM1,VHOST,ADMET007,DAILY_1800_VM_SDC-CTL-PROD3,NO
AIXTSM2,VHOST,ADMET014,DAILY_1900_VM_UDC-CTL-PROD,YES
AIXTSM1,VHOST,AGGREGATE,DAILY_2200_VM_SDC-CTL-PROD5,NA

在 18th F2 上执行代码后的预期结果:输入文件 1

TSM,TYPE,NODE,SCHED,2018-11-17,2018-11-18
AIXTSM1,VHOST,10.199.114.72,DAILY_1800_VM_SDC-CTL-PROD,YES,YES
AIXTSM1,VHOST,ADMET007,DAILY_1800_VM_SDC-CTL-PROD3,NO,NO
AIXTSM2,VHOST,ADMET014,DAILY_1900_VM_UDC-CTL-PROD,YES,YES
AIXTSM1,VHOST,AGGREGATE,DAILY_2200_VM_SDC-CTL-PROD5,NA,NA

代码

awk -F, -v date=$(date +'%Y-%m-%d') ' BEGIN   { OFS = FS } FNR==NR { a[] = ; next } FNR==1  { n1 = n = NF + 1; $n = date; print; next } { $n1 = ( in a) ? a[] : "NA"; print }' f2 f1 > t && mv -f t f1

以上代码的结果不正确

看起来你在预期输出中显示的最后一行看起来不符合你显示的规则,请你尝试遵循。

awk -F, -v DAT=$(date +'%Y-%m-%d') '
FNR!=NR && FNR==1{
  print [=10=]","DAT
  next
}
FNR==NR{
  a[,,,]=[=10=]
  next
}
{
  [=10=]=((,,,) in a)?a[,,,]:[=10=]"," $NF ",NA"
}
1
'  Input_file2   Input_file1

如果您想将输出保存到 Input_file 本身,请附加 > temp_file && mv temp_file Input_file