使用awk从多个文本文件中提取列

时间:2012-06-05 10:53:40

标签: awk

我正在尝试根据column2的值提取column1。我想仅在column2≤30且大于5时才打印column1的值。

我还需要根据输出打印column1的值总数。如何使用awk从多个文本文件中执行此操作?

文本文件示例如下所示。

col1   col2  

aa     25
bb     4
cc     6
dd     23
aa     30

输出为

aa
cc
dd
aa

Total number of aa is 2
Total number of cc is 1
Total number of dd is 1

1 个答案:

答案 0 :(得分:3)

这样的事情让你开始:

{ if ($2 <= 30 && $2 > 5) {
    print $1
    tot[$1] += 1 }
}
END {

  for (i in tot) {
    print "Total number of", i, "is", tot[i]
  }
}

输出:

$ awk -f i.awk input
aa
cc
dd
aa
Total number of aa is 2
Total number of cc is 1
Total number of dd is 1