awk-通过比较字段来操作文件

awk-通过比较字段来操作文件

我有2个文件如下:

节_ne.csv

NE_section,,,,,,,,,,,,,,,,,,,,,,,
D,NE Name,IP Address,Site Name,en Product Legacy Code,en Product Legacy Revision,Product Model,NE Status,company Serial Number,company SW Major Release,company SW Minor Release,company SW Patch Release,Manufacturing Date,Installation Date ,Into Service Date ,Licensed System ID,Licensed Key,Decommissioning Date,Description,,,,,
X,ts,1.1.1.26,,,,Linux,,,9.6.02,356349,,,,,,,,A,,,,,
X,ts_I,1.1.1.26,,,,Linux,,,9.6.02,356272,,,,,,,,I,,,,,
X,ts_O,1.1.1.26,,,,Linux,,,9.6.01,348219,,,,,,,,O,,,,,
X,ts_S,1.1.1.26,,,,Linux,,,9.6.02,356272,,,,,,,,S,,,,,
X,1.1.88.83,1.1.88.83,,,,O-M,,,4.8,a7gpaa48.766,,,,,,,,,,,,,
X,is180,10.5.5.180,,,,N-D,,,5.2,a6gpaa52.604,,,,,,,,,,,,,
X,is171,10.5.5.171,,,,N-F,,,5.4,a6gpaa54.431,,,,,,,,,,,,,
X,is174,10.5.5.174,,,,N-F,,,5.4,a6gpaa54.431,,,,,,,,,,,,,
X,Detected_at_10.5.5.106,10.5.5.106,,,,N-B,,,5.5,a6gpaa55.568,,,,,,,,,,,,,
X,is112,10.5.5.112,,,,N-A,,,5.5,a6gpaa55.574,,,,,,,,,,,,,
X,is107,10.5.5.107,,,,N-A,,,5.6,a6gpaa56.561,,,,,,,,,,,,,
X,172.29.7.227,172.29.7.227,,,,N-D,,,5.6,a6gpaa56.175,,,,,,,,,,,,,
X,Detected_at_10.5.5.121,10.5.5.121,,,,N-A,,,5.6,a6gpaa56.462,,,,,,,,,,,,,
X,is172,10.5.5.172,,,,N-F,,,5.6,a6gpaa56.561,,,,,,,,,,,,,
X,is179,10.5.5.179,,,,N-D,,,5.6,a6gpaa56.561,,,,,,,,,,,,,
X,is181,10.5.5.181,,,,N-D,,,5.6,a6gpaa56.453,,,,,,,,,,,,,
X,eyup,10.5.5.91,,,,C-A,,,5.6,a6gpaa56.557,,,,,,,,,,,,,
...

查找.csv

Patch_release,Build
5.7.02,
5.7.01e,57.345
5.7.01,57.324
5.7.00c,57.161
5.7.00a,57.160
5.7,57.145
5.6.02d,
5.6.02b,56.574
5.6.02a,56.562
5.6.02,56.561
5.6.01j,56.463
5.6.01g,56.476
5.6.01f,
5.6.01e,56.488
5.6.01c,56.462
5.6.01b,56.453
5.6.01a,56.452
5.6.01,56.439
5.6.00a,56.157
5.6,56.153
5.5.02h,
5.5.02g,55.598
...

我想做以下事情:

  • 与 awk 比较这些文件
  • 如果section_ne.csv的第11个字段(公司软件次要版本)包含lookup.csv中列出的任何版本(位于第二列),则从lookup.csv表中获取匹配的Patch_release值并更新section_ne.csv文件
  • 将此 Patch_release 值放入section_ne.csv(公司软件补丁版本)的第 12 个字段。

例如:

NE_section,,,,,,,,,,,,,,,,,,,,,,,
D,NE Name,IP Address,Site Name,en Product Legacy Code,en Product Legacy Revision,Product Model,NE Status,company Serial Number,company SW Major Release,company SW Minor Release,company SW Patch Release,Manufacturing Date,Installation Date ,Into Service Date ,Licensed System ID,Licensed Key,Decommissioning Date,Description,,,,,
...
    X,is107,10.5.5.107,,,,N-A,,,5.6,a6gpaa56.561,,,,,,,,,,,,,
...

此条目将与lookup.csv 文件中的以下条目匹配:

5.6.02,56.561

然后section_ne.csv文件中的相关条目将更新如下:

X,is107,10.5.5.107,,,,N-A,,,5.6,a6gpaa56.561,5.6.02,,,,,,,,,,,,

答案1

这是你的主意吗?一个简单的肮脏的例子:

$ sort -V -t , -k 1 lookup.csv > lookup_sorted.csv 
$ sort -V -t , -k 10 section_ne.csv > section_ne_sorted.csv 
$ join -1 10 -2 1 -t , section_ne_sorted.csv lookup_sorted.csv 
5.6,X,172.29.7.227,172.29.7.227,,,,N-D,,,a6gpaa56.175,,,,,,,,,,,,,,56.153
5.6,X,is181,10.5.5.181,,,,N-D,,,a6gpaa56.453,,,,,,,,,,,,,,56.153
5.6,X,Detected_at_10.5.5.121,10.5.5.121,,,,N-A,,,a6gpaa56.462,,,,,,,,,,,,,,56.153
5.6,X,eyup,10.5.5.91,,,,C-A,,,a6gpaa56.557,,,,,,,,,,,,,,56.153
5.6,X,is107,10.5.5.107,,,,N-A,,,a6gpaa56.561,,,,,,,,,,,,,,56.153
5.6,X,is172,10.5.5.172,,,,N-F,,,a6gpaa56.561,,,,,,,,,,,,,,56.153
5.6,X,is179,10.5.5.179,,,,N-D,,,a6gpaa56.561,,,,,,,,,,,,,,56.153
join: file 2 is not in sorted order
join: file 1 is not in sorted order

答案2

我会使用 join 命令

连接·描述 对于每对具有相同连接字段的输入行,将一行写入标准输出。默认连接字段是第一个,由空格分隔。当 FILE1 或 FILE2(不是两者)为 - 时,读取标准输入。

加入

在以下网站中点击这里 您可以找到 csv 文件的示例。

相关内容