如何查找文本文件中多个单词的数量? [英] how do i find the count of multiple words in a text file?

查看:184
本文介绍了如何查找文本文件中多个单词的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够找到一个文本文件中单词出现的次数,例如在Linux中,我们可以使用

i am able to find the number of times a word occurs in a text file like in Linux we can use

cat filename|grep -c tom

我的问题是如何在文本文件中找到多个单词的计数,例如"tom"和"joe".

my question is how do i find the count of multiple words like "tom" and "joe" in a text file.

推荐答案

由于您有几个名字,因此使用正则表达式是可行的方法.起初我以为只是对joe或tom的正则表达式进行grep计数就这么简单,但是发现这并没有考虑到tom和joe处于同一行(或者就此而言,tom和tom)的情况. .

Since you have a couple names, regular expressions is the way to go on this one. At first I thought it was as simple as just a grep count on the regular expression of joe or tom, but fount that this did not account for the scenario where tom and joe are on the same line (or tom and tom for that matter).

test.txt:

tom is really really cool!  joe for the win!
tom is actually lame.


$ grep -c '\<\(tom\|joe\)\>' test.txt
2

从test.txt文件中可以看到,答案2是错误的,因此我们需要考虑名称在同一行.

As you can see from the test.txt file, 2 is the wrong answer, so we needed to account for names being on the same line.

然后,我使用grep -o仅显示与模式匹配的匹配行部分,该部分在文件中给出了与tom或joe正确的模式匹配.然后,我将结果通过管道传输到wc中,以获取行数.

I then used grep -o to show only the part of a matching line that matches the pattern where it gave the correct pattern matches of tom or joe in the file. I then piped the results into number of lines into wc for the line count.

$ grep -o '\(joe\|tom\)' test.txt|wc -l
       3

3 ...正确答案!希望这会有所帮助

3...the correct answer! Hope this helps

这篇关于如何查找文本文件中多个单词的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆