【问题标题】:How to count the occurrence of an element in each line in a file (bash)如何计算文件中每一行中某个元素的出现次数(bash)
【发布时间】:2021-01-28 20:38:44
【问题描述】:

我有一个如下所示的文件:

1|2|3|4
1|2|3|4
1|2|3
1|2
1|2|3|4
1|2|3|4

我要做的是计算| 在每一行中出现的频率并打印如下消息:

4 line(s) have 3 occurrence(s) of |
1 line(s) have 2 occurrence(s) of |
1 line(s) have 1 occurrence(s) of |

我一直在使用此代码grep -o '|' filename | wc -l,但它计算了| 在整个文件中出现的次数,而不仅仅是在每一行中。我是 bash 新手,非常感谢您的帮助!

【问题讨论】:

    标签: bash awk grep structure subject


    【解决方案1】:

    你可以使用这个awk:

    awk -F "|" '{++fq[NF-1]} END {for (f in fq) printf "%d line(s) have %d occurrence(s) of %s\n", fq[f], f, FS}' file
    
    1 line(s) have 1 occurrence(s) of |
    1 line(s) have 2 occurrence(s) of |
    4 line(s) have 3 occurrence(s) of |
    

    为了使其更具可读性:

    awk -F "|" '{
       ++fq[NF-1]
    }
    END {
    for (f in fq)
       printf "%d line(s) have %d occurrence(s) of %s\n", fq[f], f, FS
    }' file
    

    【讨论】:

      【解决方案2】:

      您也可以使用bash arrays,使用如下脚本:

      #/bin/bash
      # Variables
      file="$1"
      declare -a results
      # Scanning
      while IFS='|' read -a line; do
        n=$((${#line[@]} - 1))
        results[$n]=$((${results[$n]} + 1))
      done <"$file"
      # Printing
      for n in ${!results[@]}; do
        echo ${results[$n]} lines\(s\) have $n occurences of \|
      done
      

      【讨论】:

        【解决方案3】:

        对于您显示的示例,您能否尝试以下操作。

        awk '
        {
          count[gsub(/\|/,"&")]++
        }
        END{
          for(i in count){
            print count[i] " line(s) have "i " occurrence(s) of |"
          }
        }'  Input_file
        

        输出如下。

        1 line(s) have 1 occurrence(s) of |
        1 line(s) have 2 occurrence(s) of |
        4 line(s) have 3 occurrence(s) of |
        

        【讨论】:

          猜你喜欢
          • 2010-10-05
          • 2017-08-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多