Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grep only words which consist of capital characters

Tags:

regex

grep

I have problem write grep which should grep only those lines, in which is word that consist only from capital characters.

For example I have file : file1.txt

Abc AAA
ADFSD
F
AAAAx

And output should be :

Abc AAA
ADFSD
F

Thank for any advice.

like image 497
Tempus Avatar asked Oct 08 '13 17:10

Tempus


4 Answers

You can just use:

grep -E '\b[[:upper:]]+\b' file1.txt

That is, look for whole words composed of only uppercase letters.

like image 102
Carl Norum Avatar answered Oct 18 '22 09:10

Carl Norum


This egrep should work:

egrep '\b[A-Z]+\b' file
like image 29
anubhava Avatar answered Oct 18 '22 10:10

anubhava


This will produce the desired results,

egrep '\b[A-Z]+\b'  file1.txt

Results are

Abc AAA
ADFSD
F
like image 27
CS Pei Avatar answered Oct 18 '22 10:10

CS Pei


GNU grep supports POSIX patterns, so you can simply do:

grep -e '[[:upper:]]' file1.txt

like image 37
Elias Probst Avatar answered Oct 18 '22 09:10

Elias Probst