Awk noob is here. I digested Stephen's answer and as a result, with this, we hope it is easier to understand the fragment below. There are two more subtle issues:
- Awk array is actually a dictionary. This is not
["value1", "value2"] , it is more like {0: "value1", 1: "value2"} . in checks keys, and there is no built-in way to check values.
So, you need to convert your array (which is actually a dictionary) into a dictionary with values ββin the form of keys.
BEGIN { split("value1 value2", valuesAsValues) # valuesAsValues = {0: "value1", 1: "value2"} for (i in valuesAsValues) valuesAsKeys[valuesAsValues[i]] = "" # valuesAsKeys = {"value1": "", "value2": ""} } # Now you can use `in` ($1 in valuesAsKeys) {print}
For single line:
echo "A:B:C:D:E:F" | tr ':' '\n' | \ awk 'BEGIN{ split("ADF", parts); for (i in parts) dict[parts[i]]=""} $1 in dict'
source share