grep whole words made of only uppercase letters

前端 未结 5 1966
[愿得一人]
[愿得一人] 2021-01-23 04:23

Seems like this is rather simple, but I\'m having trouble.

I have a text document that looks, for example, like this:

This is a
TEXT DOCUME

5条回答
  •  星月不相逢
    2021-01-23 05:10

    To complement Zbynek Vyskovsky - kvr000's helpful answer:

    grep's -E option allows use of extended regular expression, which includes quantifier + to mean one or more, which simplifies the solution:

     grep -Eo '\<[[:upper:]]+\>' Untitled.txt
    

    Also, as mentioned in Benjamin W.'s answer, -w can be used to match on word boundaries without having to specify it as part of the regex:

     grep -Ewo '[[:upper:]]+' Untitled.txt
    

    Note, however, that -w is a nonstandard option (but both BSD/OSX and GNU grep implement it).


    As for egrep: it is nothing more than an (effective) alias of grep -E, which, as stated, activates support for extended regular expressions, but the exact set of features is platform-dependent.

    Additionally, only GNU grep supports the -P option to support PCREs (Perl-Compatible Regular Expression), which offer even more features and flexibility.

提交回复
热议问题