如何编写一个 Shell 脚本来计算文件中每个数字的出现次数?

如何编写一个 Shell 脚本来计算文件中每个数字的出现次数?

今天面试官问了这个问题。假设有一个名为“Myfile.txt”的文件,其内容如下:(不一定在一行)

1 3 4
4 1 5
1 9 8
3 2 1
6 0 0
3 4 5
7 8 9

我想编写一个脚本来告诉我哪个数字在该文件中出现了多少次。您可以看到它包含从 0 到 9 的数字。正如您所看到的,“1”在该文件中重复了 4 次,输出应该告诉我“数字 1 在该文件中使用了 4 次”。

答案1

如果文件每行可以有多个数字,那么先将其更改为每行一个然后再计数会更简单。例如:

$ tr ' ' '\n' < file| sort | uniq -c
  2 0
  4 1
  1 2
  3 3
  3 4
  2 5
  1 6
  1 7
  2 8
  2 9

如果您确实需要详细输出,您可以进一步将其解析为:

$ tr ' ' '\n' < file| sort | uniq -c | while read cnt num; do printf 'The number %s appears %s times in the file\n' "$num" "$cnt"; done
The number 0 appears 2 times in the file
The number 1 appears 4 times in the file
The number 2 appears 1 times in the file
The number 3 appears 3 times in the file
The number 4 appears 3 times in the file
The number 5 appears 2 times in the file
The number 6 appears 1 times in the file
The number 7 appears 1 times in the file
The number 8 appears 2 times in the file
The number 9 appears 2 times in the file

或者:

$ tr ' ' '\n' < file| sort | uniq -c | awk '{print "The number "$2" appears "$1" times in the file"}'
The number 0 appears 2 times in the file
The number 1 appears 4 times in the file
The number 2 appears 1 times in the file
The number 3 appears 3 times in the file
The number 4 appears 3 times in the file
The number 5 appears 2 times in the file
The number 6 appears 1 times in the file
The number 7 appears 1 times in the file
The number 8 appears 2 times in the file
The number 9 appears 2 times in the file

答案2

$ awk -v RS='[[:space:]]+' \
     '{ n[$1]++ };

      END {
        for (i in n) {
          print i":",n[i]
        }
      }' debasish.txt 

(这是一行行,添加了换行符和缩进以提高可读性)

将记录分隔符 ( RS) 设置为 1 个或多个任何类型的空白(空格、制表符、换行符等),然后对数组中出现的每个数字进行计数n。打印n输入末尾每个元素的总计。

输出:

0: 2
1: 4
2: 1
3: 3
4: 3
5: 2
6: 1
7: 1
8: 2
9: 2

相关内容