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.
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.
You can just use:
grep -E '\b[[:upper:]]+\b' file1.txt
That is, look for whole words composed of only uppercase letters.
This egrep should work:
egrep '\b[A-Z]+\b' file
This will produce the desired results,
egrep '\b[A-Z]+\b' file1.txt
Results are
Abc AAA
ADFSD
F
GNU grep supports POSIX patterns, so you can simply do:
grep -e '[[:upper:]]' file1.txt
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"
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
.