简体   繁体   English

awk中“ in”函数的含义

[英]the meaning of “in” function in awk

I don't really understand the phrase in in awk code, ie 我真的不明白这句话in的AWK代码,即

awk -F'|' 'NR>1{
      band[$2]++
  }
  END{
    print "Band | Sum"
      for (x in band){
        print x" | " band[x]
      }
  }'

and

awk -F"|" 'NR==FNR{a[$1]=$2;next}{for(i=3;i<=NF;i++){if($i in a)sub($i,a[$i],$i)}print}' OFS="|" lookupfile contentfile

what does it do if the phrase is if($x in array) and for(x in array) ? 如果短语是if($x in array)for(x in array)怎么办?

From man awk : 来自man awk

The special operator in may be used to test if an array has an index consisting of a particular value: in中的特殊运算符可用于测试数组是否具有由特定值组成的索引:

if (val in array)
   print array[val]

If the array has multiple subscripts, use (i, j) in array. 如果数组有多个下标,请在数组中使用(i,j)

The in construct may also be used in a for loop to iterate over all elements of an array. in构造也可以在for循环中使用,以迭代数组的所有元素。


So your script: 所以你的脚本:

awk -F'|' 'NR>1{
      band[$2]++
  }
  END{
    print "Band | Sum"
      for (x in band){
        print x" | " band[x]
      }
  }'

Does the following: 请执行以下操作:

  • band[$2]++ Creates an array band in which you store a counter of how many times each 2nd field appears. band[$2]++创建一个数组band在其中存储一个计数器,该计数器记录每个第二个字段出现多少次。
  • for (x in band) loop through the elements in the array band to do some stuff with them. for (x in band)遍历数组band的元素以对其进行处理。 Note the for (x in band) allows you to do something with the element x as index and band[x] as value. 请注意, for (x in band)使您可以将元素x作为索引并将band[x]作为值进行操作。

its just like any other language within a for loop. 就像for循环中的任何其他语言一样。 The "in" just means you have a counter and it will go through each element within "band" “ in”仅表示您有一个计数器,它将通过“ band”中的每个元素

so when you print band[x] it is like saying print the element IN band where ever the counter is. 因此,当您打印band [x]时,就像说在计数器所在的位置打印元素IN band一样。

in means two different things in those case. in那种情况下in意思是两个不同的东西。 First: 第一:

if($i in a)sub($i,a[$i],$i)}print

In this form, $i in a is a logical test. 以这种形式, $i in a是逻辑测试。 It returns true if $i is one of the keys of array a and returns false otherwise. 如果$i是数组a的键之一,则返回true,否则返回false。

Second: 第二:

for (x in band){
        print x" | " band[x]
      }

In this form, the statement print x" | " band[x] is run once for x assigned to each of the keys of the array a . 以这种形式,语句print x" | " band[x]对于分配给数组a每个键的x运行一次。

Examples 例子

Consider a file with the contents: 考虑一个包含以下内容的文件:

abc
def
hij

Now, let's run an awk command with the for (x in a) construct: 现在,让我们使用for (x in a)构造运行awk命令:

$ awk '{a[$1]=NR} END{for (x in a) print "a[" x "]="a[x]}' file
a[def]=2
a[hij]=3
a[abc]=1

The first part, a[$1]=NR creates an array a whose keys are the first word on the line, $1 , and whose values are the number of the record (line) on which the word occurred, NR . 第一部分, a[$1]=NR创建一个数组a其键是行$1上的第一个单词,其值是出现该单词的记录(行)的编号NR

The for loop goes over every key of the array. for循环遍历数组的每个键。 It does not go through them, though, in any particular order. 但是,它不会以任何特定顺序通过它们。

Now, let's add one if condition to the above code. 现在,让我们在上面的代码中添加一个if条件。 We will use if ("klm" in a) {...} : 我们将使用if ("klm" in a) {...}

$ awk '{a[$1]=NR} END{if ("klm" in a) {for (x in a) print "a[" x "]="a[x]}}' file

The above produces no output. 上面没有产生输出。 This is because array a has not such key as klm . 这是因为数组a没有像klm这样的键。

If, instead, we had written if ("abc" in a) {...} , then there would be output: 相反,如果我们写了if ("abc" in a) {...} ,那么将输出:

$ awk '{a[$1]=NR} END{if ("abc" in a) {for (x in a) print "a[" x "]="a[x]}}' file
a[def]=2
a[hij]=3
a[abc]=1

Fortunately there's a book that explains it all, Effective Awk Programming, Third Edition, by Arnold Robbins. 幸运的是,有一本书解释了这一切,有效的Awk编程,第三版,由Arnold Robbins撰写。 You'll be interested in these sections of it: 您将对其的以下部分感兴趣:

http://www.gnu.org/software/gawk/manual/gawk.html#Reference-to-Elements http://www.gnu.org/software/gawk/manual/gawk.html#Reference-to-Elements

http://www.gnu.org/software/gawk/manual/gawk.html#Scanning-an-Array http://www.gnu.org/software/gawk/manual/gawk.html#Scanning-an-Array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM