如何计算文件中每一行中某个元素的出现次数(bash)

How to count the occurrence of an element in each line in a file (bash)

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

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 的新人,非常感谢您的帮助!

您可以使用这个 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

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

#/bin/bash
# Variables
file=""
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

根据您展示的示例,请您尝试以下操作。

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 |