【问题标题】:Three awk calls in 11 个 awk 调用
【发布时间】:2022-01-08 12:25:40
【问题描述】:

以下按预期工作:

awk '$1 <= -700 { print $3 }' FS="," tmp | awk '!seen[$0]++'

23
60
73
91

现在我计算这四个值并打印数字 4:

awk '$1 <= -700 { print $3 }' FS="," tmp | awk '!seen[$0]++' | awk '{ count++ } END { print count }'

4

有没有更短的方法来一次调用这三个 awk 调用?

非常感谢提示,

【问题讨论】:

    标签: bash awk


    【解决方案1】:

    像这样:

    awk '$1 <= -700 && !seen[$3]++ {c++} END{print c+0}' FS="," tmp
    

    解释:

    # If column 1 <= -700 and we've not seen the value of column 3 yet ...
    $1 <= -700 && !seen[$3]++ {
       # ... increment the counter c
       c++
    }
    
    # When the end of the input file is reached, print the counter
    END {
        # Note: incrementing the counter by 0 ensures that c
        # has the value 0 when no line matched the criterias and thereby
        # c has never been incremented. Without this, c would be an
        # empty string. This gets often forgotten. Thanks @Ed Morton!
    
        # Alternatively you may run the program as awk -v c=0 ...
        print c+0 
    }
    

    【讨论】:

      【解决方案2】:

      计数值?只需将值放入数组并打印长度即可,无需打印任何内容。

      awk '$1 <= -700 { uniq[$3] } END{ print length(uniq) }'
      

      【讨论】:

      • 先过滤tmp文件,然后得到唯一值,最后awk统计值。刚刚尝试了您的代码,但没有成功。
      • @KamilCuk 为什么你选择不通知AWK , 应该被视为字段分隔符?
      • @Daweo 我希望这是显而易见的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      • 2019-03-01
      • 2018-08-20
      • 1970-01-01
      • 1970-01-01
      • 2012-12-28
      • 2021-10-19
      相关资源
      最近更新 更多