user5154782
user5154782

Reputation:

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. 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. Would that work? Thanks.

Upvotes: 1

Views: 2943

Answers (4)

John1024
John1024

Reputation: 113864

Actually, I believe that you want wc, not rc, as in:

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. You can decide whether y or not it should be removed.

Also, I assumed above that your file has one word per line.

The grep options used above are as follows:

  • -v means exclude matching lines

  • -i makes the matching case-insensitive

  • -c tells grep to return a count, not the actual matches

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. -o tells grep to print only the matching portion of the line, not the entire. Because -c counts the number of lines with matches, it is not useful here. wc -l is used instead to count matches.

Upvotes: 2

John B
John B

Reputation: 3646

You could, alternatively, do this all within an 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

Upvotes: 0

dinox0r
dinox0r

Reputation: 16039

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

Upvotes: 1

Wizek
Wizek

Reputation: 4993

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:

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

Upvotes: 0

Related Questions