AWK通过在多个阵列for循环迭代 [英] Awk Iterate through several Arrays in a for loop

查看:945
本文介绍了AWK通过在多个阵列for循环迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个awk程序要经过一个文件的列数和每个不同的单词,然后再输出总数为独立的文件

I have created an awk program to go through the columns of a file and count each distinct word and then output totals into separate files

awk -F"$delim" {Field_Arr1[$1]++; Field_Arr2[$2]++; Field_Arr3[$3]++; Field_Arr4[$4]++}; 
END{\
    #  output fields
    out_field1="top_field1"
    out_field2="top_field2"
    out_field3="top_field3"
    out_field4="top_field4"

    for( i=1; i <= NF; i++)
    {
        for (element in Field_Arr$i) 
        {
            print element"\t"Field_Arr$i[element] >>out_field$i;
        }
    }
}' inputfile

但我不知道适当的语法,使循环将通过Field_Arr1,Field_Arr2,Field_Arr3,Field_Arr4迭代?

but I don't know the appropriate syntax, so that the for loop will iterate through Field_Arr1, Field_Arr2, Field_Arr3, Field_Arr4?

我已经尝试使用我,$ I $ {I},{I},$ I和i。

I have tried using: i, $i, ${i}, {i}, "$i", and "i".

我是否尝试错误的做法还是有办法改变Field_Arr $ i到Field_Arr1..4?

Am I trying the wrong approach or is there a way to change Field_Arr$i to Field_Arr1..4?

谢谢你的建议。

推荐答案

AWK 变量不工作的方式;你必须通过名字逐个做出来,或者用假多维数组,并解析出部件,沿着线的东西:

awk variables don't work that way; you'll have to do them individually by name, or use fake multidimensional arrays and parse out the components, something along the lines of:

{Field_Arr[1, $1]++; Field_Arr[2, $2]++; Field_Arr[3, $3]++; Field_Arr[4, $4]++}
END {
  for (elt in Field_Arr) {
    split(elt, ec, SUBSEP)
    print ec[2] "\t" Field_Arr[elt] >> ("top_field" ec[1])
  }
}

这篇关于AWK通过在多个阵列for循环迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆