简体   繁体   English

在Unix中使用grep和管道来查找特定的单词

[英]Using grep and pipes in Unix to find specific words

Let's say I'm using grep , and I use use the -v option on a text file to find all the words that do not contain vowels. 假设我正在使用grep ,并且我在文本文件中使用-v选项来查找所有不包含元音的单词。 If I then wanted to see how many words there are in this file that do not contain vowels, what could I do? 如果我想查看此文件中有多少单词不包含元音,我该怎么办?

I was thinking of using a pipe and using the rc command by itself. 我正在考虑使用管道并单独使用rc命令。 Would that work? 那会有用吗? Thanks. 谢谢。

Actually, I believe that you want wc , not rc , as in: 实际上,我相信你想要wc ,而不是rc ,如:

grep -civ '[aeiouy]' words.txt

For example, consider the file: 例如,考虑文件:

$ cat words.txt
the
words
mph
tsk
hmmm

Then, the following correctly counts the three "words" without vowels: 然后,以下正确计算没有元音的三个“单词”:

$ grep -civ '[aeiouy]' words
3

I included y in the vowel list. 我在元音列表中包含了y You can decide whether y or not it should be removed. 你可以决定是否y或不应该被删除。

Also, I assumed above that your file has one word per line. 另外,我假设你的文件每行有一个单词。

The grep options used above are as follows: 上面使用的grep选项如下:

  • -v means exclude matching lines -v表示排除匹配行

  • -i makes the matching case-insensitive -i使匹配不区分大小写

  • -c tells grep to return a count, not the actual matches -c告诉grep返回一个计数,而不是实际的匹配

Multiple words per line 每行多个单词

$ echo the tsk hmmm | grep -io '\b[bcdfghjklmnpqrstvxz]*\b' | wc -l
2

Because \\b matches at word boundaries, the above regex matches only words that lack vowels. 因为\\b匹配单词边界,所以上面的正则表达式只匹配缺少元音的单词。 -o tells grep to print only the matching portion of the line, not the entire. -o告诉grep只打印行的匹配部分,而不是整个。 Because -c counts the number of lines with matches, it is not useful here. 因为-c计算匹配的行数,所以在这里没用。 wc -l is used instead to count matches. 改为使用wc -l来计算匹配。

The following script will count the number of words that don't contain vowels (if there are several words per line): 以下脚本将计算不包含元音的单词数(如果每行有多个单词):

#!/bin/bash

# File can be a script parameter
FILE="$1"

let count=0
while read line; do
    for word in $line; do
        grep -qv "[aeiou]" <<< "$word"
        if [ $? -eq 0 ]; then
           let count++
        fi
    done
done < FILE
echo "words without vowels: $count"

If there is only one word per line, then the following will be enough: 如果每行只有一个单词,那么以下就足够了:

grep -cv "[aeiou]" < file

If multiple words can be on the same line, and you want to count them too, you can use grep -o with wc -l to count all the matches correctly, like so: 如果多个单词可以在同一行,并且您也想对它们进行计数,则可以使用grep -owc -l来正确计算所有匹配项,如下所示:

$ echo "word work no-match wonder" | grep -o "wo[a-z]*" | wc -l
3

You could, alternatively, do this all within an Awk: 或者,您可以在Awk中完成所有操作:

awk '!/[aeiou]/ {n++} END {print n}' file

For lines with multiple fields: 对于具有多个字段的行:

awk '{for(i=1; i<=NF; i++) if($i !~ /[aeiou]/) n++} END {print n}' file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM