且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过将字段与bash中的另一个csv文件进行匹配来在csv文件中添加列

更新时间:2023-08-27 13:42:52

如果您的输入文件是这样的,即第一个版本,逗号后面有空格:

If your input files look like this, i.e. the first version, with spaces after the comma:

File1.csv

File1.csv

Host, IP
PC1, 192.168.3.1
PC2, 192.168.3.2
PC3, 192.168.3.3

并且:

File2.csv

File2.csv

Port, Type, S_Host, S_IP, Port, D_Host, D_IP, Port
2, tcp, N/A, 192.168.3.1, 2, N/A, 192.168.3.2, 8
3, tcp, N/A, 192.168.3.2, 2, N/A, 192.168.3.3, 3

尝试:

#!/bin/bash
awk '
  BEGIN {FS = ", "; OFS = ", "}
  (FNR == NR) && (NR > 1) {hh[$2] = $1}
  NR > FNR {
    if (FNR == 1) 
      print; 
    else 
      print $1, $2, hh[$4], $4, $5, hh[$7], $7, $8;
  }
' File1.csv File2.csv

这是我的输出获取:

Port, Type, S_Host, S_IP, Port, D_Host, D_IP, Port
2, tcp, PC1, 192.168.3.1, 2, PC2, 192.168.3.2, 8
3, tcp, PC2, 192.168.3.2, 2, PC3, 192.168.3.3, 3




此外,如果IP是公共IP,则我需要进行Whois搜索以获取OrgName

Also, if the IP is a public IP, I need to do a whois search instead to get the OrgName

我建议您发布有关此第二个主题的另一个问题。就像在专业电子邮件中一样:一项等于一个问题。

I suggest you to post another question about this second topic. It is like in professional emails: one item = one question.