检查awk数组是否包含值

时间:2022-06-15 00:58:47

With Perl you can check if an array contains a value

使用Perl,您可以检查数组是否包含值

$ 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,这个类似的命令是检查数组索引而不是值

$ 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?

如何使用awk检查数组是否包含特定值?

2 个解决方案

#1


7  

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:

Awk noob在这里。我消化了史蒂文的答案,最后有了这个希望更容易理解下面的片段。还有2个微妙的问题:

  • An Awk array is actually a dictionary. It's not ["value1", "value2"], it's more like {0: "value1", 1: "value2"}.
  • Awk数组实际上是一个字典。它不是[“value1”,“value2”],它更像是{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'

#2


4  

Based on Thor’s comment, this function does the trick for me:

基于Thor的评论,这个功能对我有用:

function smartmatch(diamond, rough,   x, y) {
  for (x in rough) y[rough[x]]
  return diamond in y
}
BEGIN {
  split("444 555 666", z)
  print smartmatch(555, z) ? "T" : "F"
}

#1


7  

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:

Awk noob在这里。我消化了史蒂文的答案,最后有了这个希望更容易理解下面的片段。还有2个微妙的问题:

  • An Awk array is actually a dictionary. It's not ["value1", "value2"], it's more like {0: "value1", 1: "value2"}.
  • Awk数组实际上是一个字典。它不是[“value1”,“value2”],它更像是{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'

#2


4  

Based on Thor’s comment, this function does the trick for me:

基于Thor的评论,这个功能对我有用:

function smartmatch(diamond, rough,   x, y) {
  for (x in rough) y[rough[x]]
  return diamond in y
}
BEGIN {
  split("444 555 666", z)
  print smartmatch(555, z) ? "T" : "F"
}