21

With Perl you can check if an array contains a value

$ perl -e '@foo=(444,555,666); print 555 ~~ @foo ? "T" : "F"'
T

However with awk, this similar command is checking the array indexes rather than values

$ awk 'BEGIN {split("444 555 666", foo); print 555 in foo ? "T" : "F"}'
F

How can I check if an array contains a particular value with awk?

codeforester
  • 34,080
  • 14
  • 96
  • 122
Zombo
  • 1
  • 55
  • 342
  • 375
  • 1
    I believe your only option is to loop. – Etan Reisner Nov 04 '14 at 22:40
  • 4
    Like Etan says, you need a for-loop. You can achieve a somewhat flexible result by creating a new array (hash) with the values as keys, e.g.: `awk 'BEGIN { split("444 555 666", foo); for(i=1; i<=length(foo); i++) bar[foo[i]]; print 555 in bar ? "T" : "F" }'` – Thor Nov 04 '14 at 23:19
  • 3
    @Thor - Can't you just use `for( i in foo ) bar[foo[i]]` instead? – n0741337 Nov 04 '14 at 23:24
  • 3
    @n0741337: You are right, shorter and cleaner, so it becomes: `awk 'BEGIN { split("444 555 666", foo); for(i in foo) bar[foo[i]]; print 555 in bar ? "T" : "F" }'`. – Thor Nov 04 '14 at 23:27

3 Answers3

23

Awk noob here. I digested Steven's answer and ended up with this hopefully easier to understand snippet below. There are 2 more subtle problems:

  • An Awk array is actually a dictionary. It's not ["value1", "value2"], it's more like {0: "value1", 1: "value2"}.
  • in checks for keys, and there is no built-in way to check for values.

So you have to convert your array (which is actually a dictionary) to a dictionary with the values as 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 one-liners:

echo "A:B:C:D:E:F" | tr ':' '\n' | \
awk 'BEGIN{ split("A D F", parts); for (i in parts) dict[parts[i]]=""}  $1 in dict'
Thamme Gowda
  • 10,359
  • 3
  • 46
  • 54
phunehehe
  • 8,388
  • 7
  • 46
  • 78
0

what I do is structure my data to take advantage of what the language provides.

So instead of saying :

BEGIN {split("444 555 666", foo); .... }

say :

BEGIN { bar[ "444" ] ; ... }

or ala another suggestion:

BEGIN { 
  split("444 555 666", foo);
  for( i in FOO ) bar[foo[i]];
}

remember arrays in awk are really associative arrays.

Bob Makowski
  • 139
  • 1
  • 6
0

gawk 'BEGIN { split("a b c d",parts); for (i in parts) dict[parts[i]]=1 } $1 in dict {print}' some.log

Ryan Liu
  • 431
  • 4
  • 5