grep -c
对于查找字符串在文件中出现的次数很有用,但它每行只计算每次出现一次。如何计算每行出现的多次?
我正在寻找比以下更优雅的东西:
perl -e '$_ = <>; print scalar ( () = m/needle/g ), "\n"'
Run Code Online (Sandbox Code Playgroud) 我有一个命令可以处理一个文本文件,计算所有出现的单词并像这样打印出来:
user@box $˜ magic-command-i-forgot | with grep | and awk | sort ./textfile.txt
66: the
54: and
32: I
16: unix
12: bash
5: internet
3: sh
1: GNU/Linux
Run Code Online (Sandbox Code Playgroud)
因此,它不是逐行搜索,而是逐词搜索,而且它会搜索所有单词,而不仅仅是 1 个单词。很久以前我在互联网上的某个地方找到了它,但我找不到或不记得它了。
如何查找文件中每个单词的计数?
我想要文本管道或文档中每个单词的直方图。文档中将存在新行和空行。我把除了 之外的所有东西都脱光了[a-zA-Z]
。
> cat doc.txt
word second third
word really
> cat doc.txt | ... # then count occurrences of each word \
# and print in descending order separated by delimiter
word 2
really 1
second 1
third 1
Run Code Online (Sandbox Code Playgroud)
它需要具有一定的效率,因为文件是 1GB 文本,并且无法处理指数时间负载。
如何在 Linux 上的文件中 grep 两个不同单词的出现次数,例如“注册”和“evn”?
输出应如下所示:
registered:20
Run Code Online (Sandbox Code Playgroud)