grep for capital words

Actually with the -o switch, GNU grep returns only what it has matched. How about:

grep -oP "\w*[A-Z]+\w*" yourfile.txt

Note that this regex will match words with capital letters anywhere in them, not necessarily the beginning. You should tune it to meet your needs if they differ.

As pointed out below, this might not be the most portable of solutions. A portable alternative in Perl is

perl -nE 'say $1  while /(\w*[A-Z]+\w*)/g' yourfile.txt

you could use sed to only return the string and not the whole line

sed 's/.*\([A-Z]*\).*/\1/g' <file>

or

sed 's/[a-z]*\| //g' <file>

The POSIX Basic Regular expression standard (used by default in grep, vim, less, sed, etc.) uses \< and \> to signify word boundaries. This allows a letter to follow white-space, as well as non-alphanumeric characters like quotes, dashes, equal-signs, etc. Use the -o option to print each match on a new line, et voila:

grep -o '\<[A-Z][a-z]*\>' yourfile.txt

Again, you might need to have change the regexp to suit your needs. Maybe by allowing numbers or a second capital letter..? This does both..

grep -o '\<[A-Z][a-z0-9]*[A-Z][a-z0-9]*\>' yourfile.txt

Tags:

Grep