Tempus
Tempus

Reputation: 240

How to grep only words which consist of capital characters

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.

Upvotes: 14

Views: 47118

Answers (7)

Carl Norum
Carl Norum

Reputation: 225042

You can just use:

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

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

Upvotes: 17

CS Pei
CS Pei

Reputation: 11047

This will produce the desired results,

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

Results are

Abc AAA
ADFSD
F

Upvotes: 4

Jirka
Jirka

Reputation: 4213

If your input contains non-ASCII characters, you may want to use \p{Lu} instead of [A-Z]:

grep -P '\b\p{Lu}+\b' file

For

LONDON 
Paris
MÜNCHEN Berlin

this will return

LONDON
MÜNCHEN Berlin

You can probably list most of these things manually, and as @Skippy-le-grand-gourou says, egrep extends [A-Z] to accented letters, but by using \p{Lu}, you do not need to deal with things like "Since June 2017, however, capital ẞ is accepted as an alternative in the all-caps style"

Upvotes: 1

alhelal
alhelal

Reputation: 936

grep -oP '\b[A-Z0-9_]+\b' file1.txt  

This results words consisting of uppercase/digit/_ (e.g. HELLO, NUMBER10, RLIMIT_DATA).

But, this also accept eDw.

Upvotes: 1

dono
dono

Reputation: 17

grep '\<[A-Z]*>' file1.txt

Upvotes: 0

anubhava
anubhava

Reputation: 785581

This egrep should work:

egrep '\b[A-Z]+\b' file

Upvotes: 10

Elias Probst
Elias Probst

Reputation: 277

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

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

Upvotes: 1

Related Questions