Reputation: 2211
I want to find the number of uppercase words in a text file by using ubuntu terminal 'grep' command..
I already tried with grep -c [[:upper:]] a.txt
command. But it count words which have simple characters also.
My text: I am Tom PERERA
My command line output should 2, that the words are "I" and "PERERA".
Upvotes: 2
Views: 1018
Reputation: 61520
I believe that the -c
flag only returns the number of lines that were matched. If you have more than a single match per line it will give you incorrect results. I typically do something like this:
echo 'I am Tom PERERA' | grep -oE '\b[A-Z]+\b' | wc -l
Note: you can also use '\b[[:upper:]]+\b'
if you prefer the POSIX classes
EDIT:
As @tripleee points out, the POSIX class [:upper:]
will span locales and is a more portable solution.
Upvotes: 5