Reputation: 240
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
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
Reputation: 11047
This will produce the desired results,
egrep '\b[A-Z]+\b' file1.txt
Results are
Abc AAA
ADFSD
F
Upvotes: 4
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
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
Reputation: 277
GNU grep supports POSIX patterns, so you can simply do:
grep -e '[[:upper:]]' file1.txt
Upvotes: 1