multiple condition store in variable and use as if condition in awk

table1.csv:

33622|AAA
33623|AAA
33624|BBB
33625|CCC
33626|DDD
33627|AAA
33628|BBB
33629|EEE
33630|FFF

Aims:

33622|AAA
33623|AAA
33624|BBB
33625|CCC
33626|DDD
33627|AAA
33628|BBB

Using command:

 awk 'BEGIN{FS="|";OFS="|"} {if($2=="AAA" && $2=="BBB" && $2=="CCC" && $2=="DDD"){print $1,$2}}' table1.csv

However, trying to be more automatic, since the categories may increase.

list1.csv:
AAA BBB CCC DDD

list=`cat list1.csv`
awk -v list=$list 'BEGIN{FS="|";OFS="|"} {if($2==list){print $1,$2}}' table1.csv

Expected output:

33622|AAA
33623|AAA
33624|BBB
33625|CCC
33626|DDD
33627|AAA
33628|BBB

So, any suggestion on storing the multiple condition in one variable?

Thanks all!



Read more here: https://stackoverflow.com/questions/66346081/multiple-condition-store-in-variable-and-use-as-if-condition-in-awk

Content Attribution

This content was originally published by Noktang at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: