简体   繁体   English

AWK - 在查找文件中查找数据并打印“成功”或“失败”

[英]AWK - Lookup data in lookup file and Print "success" or "Failure"

Having trouble working out what I need to achieve in AWK.无法确定我需要在 AWK 中实现的目标。 I have 2 files:我有2个文件:

Data File:-数据文件:-

XYZ:ABC:tab1:join_check:abc111:abc.tab1234
XYZ:ABC:tab2:join_check:abc112:abc.tab1234
XYZ:ABC:tab3:join_check:abc113:abc.tab1234
XYZ:NON-ABC:tab1:join_check:abc123:abc.tab1234
XYZ:NON-ABC:tab2:join_check:abc123:abc.tab1234
HQL:ABC:tab1:join_check:abc123:abc.tab1234
HQL:NON-ABC:tab2:join_check:abc123:abc.tab1234
HQL:NON-ABC:tab3:join_check:abc123:abc.tab1234

Lookup File:-查找文件:-

XYZ:ABC:tab1
XYZ:ABC:tab2
XYZ:NON-ABC:tab1
HQL:ABC:tab1
HQL:NON-ABC:tab2

Desired Output:-所需的输出:-

XYZ:ABC:tab1:join_check:abc111:abc.tab1234:Verified
XYZ:ABC:tab2:join_check:abc112:abc.tab1234:Verified
XYZ:ABC:tab3:join_check:abc113:abc.tab1234:Failed
XYZ:NON-ABC:tab1:join_check:abc123:abc.tab1234:Verified
XYZ:NON-ABC:tab2:join_check:abc123:abc.tab1234:Failed
HQL:ABC:tab1:join_check:abc123:abc.tab1234:Verified
HQL:NON-ABC:tab2:join_check:abc123:abc.tab1234:Verified
HQL:NON-ABC:tab3:join_check:abc123:abc.tab1234:Failed

Using the First 3 Fields from Data Files, need to do lookup on the Lookup file for the same three fields.使用数据文件中的前 3 个字段,需要在 Lookup 文件中查找相同的三个字段。 If the record/entry is found (all three fields match), need to print line from Data file and append "Verified" else if record/entry is not found, need to print line from data file and append "Failed".如果找到记录/条目(所有三个字段都匹配),则需要从数据文件中打印行并附加“已验证”,否则如果未找到记录/条目,则需要从数据文件中打印行并附加“失败”。

Any help is in this regards is much appreciated非常感谢这方面的任何帮助

awk -F: -v OFS=: 'NR==FNR{lookup[$1":"$2":"$3]=1; next} {print $0, (lookup[$1":"$2":"$3] ? "Verified" : "Failed")}' lookup data

First we have two blocks of code, the first is only executed for the lookup file (first file) and the second only for the data file (second file).首先我们有两个代码块,第一个只为查找文件(第一个文件)执行,第二个只为数据文件(第二个文件)执行。 This is done by checking the FNR (file record number) and NR (overall record number).这是通过检查 FNR(文件记录号)和 NR(总记录号)来完成的。 This is only the same for the first file, so NR==FRN, and then we jump to the next line using next to prevent the second block from being executed for the first file.这只对第一个文件是一样的,所以 NR==FRN,然后我们使用next跳转到下一行,以防止对第一个文件执行第二个块。

Then for the second file, we lookup the tree fields in the lookup associative array and select "Verified" if found or "Failed" if not found using a ternary operator.然后对于第二个文件,我们在查找关联数组中查找树字段,并使用三元运算符选择“已验证”(如果找到)或“失败”(如果未找到)。

We then print the whole line $0 with that new field.然后我们用这个新字段打印整行 $0。 We set field separator and output field separator resp.我们设置字段分隔符和输出字段分隔符。 with -F: -v OFS=: .-F: -v OFS=:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM