简体   繁体   中英

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

Having trouble working out what I need to achieve in AWK. I have 2 files:

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. 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). 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.

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. We set field separator and output field separator resp. with -F: -v OFS=: .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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