计算文本文件中单词列表的出现次数 [英] Count occurrences of a list of words in a text file
本文介绍了计算文本文件中单词列表的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个文本文件,File1看起来像这样:
I have two text files, File1 looks like this:
apple
dog
cat
..
..
和File2看起来像这样:
and File2 looks like this:
appledogtree
dog
catapple
apple00001
..
..
我想计算File2中File1中单词列表的出现次数,并得到如下结果:
I want to count the occurrences of the list of words from File1 in File2, and get a result like below:
(文件1中的单词,文件2中的出现次数)
(words in File1, number of occurrences in File2)
apple 3
dog 2
cat 1
如何使用Bash命令行执行此操作?
How can I do this by using Bash command line?
推荐答案
您可以使用 fgrep
有效地做到这一点:
You can use fgrep
to do this efficiently:
fgrep -of f1.txt f2.txt | sort | uniq -c | awk '{print $2 " " $1}'
给出以下输出:
apple 3
cat 1
dog 2
-
fgrep -of f1.txt f2.txt
根据f1.txt中的模式提取f2.txt的所有匹配部分(-o
选项) -
sort |uniq -c
计算匹配模式 - 最后,
awk
交换uniq -c
输出中的单词顺序 fgrep -of f1.txt f2.txt
extracts all the matching parts (-o
option) of f2.txt based on the patterns in f1.txtsort | uniq -c
counts the matching patterns- finally,
awk
swaps the order of words inuniq -c
output
这篇关于计算文本文件中单词列表的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文