![如何查找文本文件中排除一个用户给定单词的单词数](https://linux22.com/image/120973/%E5%A6%82%E4%BD%95%E6%9F%A5%E6%89%BE%E6%96%87%E6%9C%AC%E6%96%87%E4%BB%B6%E4%B8%AD%E6%8E%92%E9%99%A4%E4%B8%80%E4%B8%AA%E7%94%A8%E6%88%B7%E7%BB%99%E5%AE%9A%E5%8D%95%E8%AF%8D%E7%9A%84%E5%8D%95%E8%AF%8D%E6%95%B0.png)
我有一大堆文本文件。其中,每篇文章均以 分隔15 stopwords
。我想找出该文件中不包括的总字数stopword
答案1
使用 GNU grep
:
grep -Eo '\S+' < file | grep -vcxF stopword
会计算 ( -c
) 单词的数量(与单词至少在有效文本上,它是不完全是 ( )的wc -w
非空格字符 ( \S+
))序列。-v
-xF
stopword
答案2
中的单词数input
减去stopword
s 的数量(使用GNU grep 的-o
,因为您标记了 Linux):
echo $(( $(wc -w < input) - $( grep -o stopword input | wc -l ) ))
输入示例:
I have the large set of the text file. In that, each article is separated by 15 stopwords. I want to find out the total number of words count in that file excluding the stopword.
stopword stopword stopword stopword stopword stopword stopword stopword stopword stopword stopword stopword stopword stopword stopword
I have the large set of the text file. In that, each article is separated by 15 stopwords. I want to find out the total number of words count in that file excluding the stopword.
输出:
$ echo $(( $(wc -w < input) - $( grep -o stopword input | wc -l ) ))
66
答案3
awk '{ gsub("stopword",""); words+=NF }; END { print words; }' /text/file
这会计算所有awk
涉及字段的内容。即使它在语义上不是一个像这样的词
- 连字符
- 空格后加一个点(句子结尾错误。下一个句子)
- 标题中的数字(1.简介)