Thursday 30 August 2018

Compare the columns of two variables and replace them by using awk or join

I have two variables which contain tabular data. Now, var1 is having one field corrupted. So I need to correct it by mapping it with another variable which holds the mapping of it.

Here in var1 2nd column is corrupted. Mapping to recover from corruption is present in var2.
Now I need to check the correct value of column2 of var1 from var2 and replace it in the result.
Contents of var1:
NODE 080e4441a6 1 BLA ENABLED BLA1 BLA2 WORKING
NODE 18628c2a24 1 BLA ENABLED BLA1 BLA2 WORKING
NODE 35c0ae9c35 1 BLA ENABLED BLA1 BLA2 WORKING
NODE 5c9dc20a1a 1 BLA ENABLED BLA1 BLA2 WORKING
NODE 7881cdedf6 1 BLA ENABLED BLA1 BLA2 WORKING
NODE 79587f053e 1 BLA ENABLED BLA1 BLA2 WORKING
NODE 859384e0cd 1 BLA ENABLED BLA1 BLA2 WORKING
NODE 8d996c83fe 1 BLA ENABLED BLA1 BLA2 WORKING
NODE 9a0fbe4290 1 BLA ENABLED BLA1 BLA2 WORKING
NODE XXX-19     1 BLA ENABLED BLA1 BLA2 WORKING
NODE YYY-20     2 BLA ENABLED BLA1 BLA2 WORKING
NODE bb2839f328 1 BLA ENABLED BLA1 BLA2 WORKING
NODE e0e440c477 1 BLA ENABLED BLA1 BLA2 WORKING
NODE ef21a2de3e 1 BLA ENABLED BLA1 BLA2 WORKING

Contents of Var2:
18628c2a24 AREA-26
e0e440c477 AREA-21
YYY-20 YYY-20
XXX-19 XXX-19
8d996c83fe AREA-24
bb2839f328 AREA-22
9a0fbe4290 AREA-23
35c0ae9c35 AREA-25
ef21a2de3e AREA-27
7881cdedf6 AREA-32
080e4441a6 AREA-28
79587f053e AREA-30
5c9dc20a1a AREA-31
859384e0cd AREA-29

Desired result:
NODE AREA-28 1 BLA ENABLED BLA1 BLA2 WORKING
NODE AREA-26 1 BLA ENABLED BLA1 BLA2 WORKING
NODE AREA-25 1 BLA ENABLED BLA1 BLA2 WORKING
NODE AREA-31 1 BLA ENABLED BLA1 BLA2 WORKING
NODE AREA-32 1 BLA ENABLED BLA1 BLA2 WORKING
NODE AREA-30 1 BLA ENABLED BLA1 BLA2 WORKING
NODE AREA-29 1 BLA ENABLED BLA1 BLA2 WORKING
NODE AREA-24 1 BLA ENABLED BLA1 BLA2 WORKING
NODE AREA-23 1 BLA ENABLED BLA1 BLA2 WORKING
NODE XXX-19 1 BLA ENABLED BLA1 BLA2 WORKING
NODE YYY-20 2 BLA ENABLED BLA1 BLA2 WORKING
NODE AREA-22 1 BLA ENABLED BLA1 BLA2 WORKING
NODE AREA-21 1 BLA ENABLED BLA1 BLA2 WORKING
NODE AREA-27 1 BLA ENABLED BLA1 BLA2 WORKING

I tried something like: but no help.
 awk 'NR==FNR{a[$1]=$2;next} ($2 in a){ gsub($2,$1)}1' <(echo "$var1") <(echo "$var2")


You were very close, try
 awk 'NR==FNR{a[$1]=$2;next}$2 in a{$2=a[$2]}1' <(echo "$var2") <(echo "$var1")

Note the order the variables are passed.

0 comments:

Post a Comment