grep for capital words

I have a file that has declarations in it with capital words, something like:

public final Foo BAR;

these declarations are not all uniform. Is there a way for me to grep out just the capital lettered words from the line? I know how to find lines that have capital letters in them, but thats not what I want. I want only the matching capital lettered word. Thanks!

Asked By: Amir Afghani


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

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


sed 's/[a-z]*| //g' <file>
Answered By: h3rrmiller

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
Answered By: Joseph R.

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
Answered By: Alex Leach
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.