Using egrep to find lines that have 5-6 Upper Case letters. (but not more than 6)

6,575

Solution 1

egrep "^[^A-Z]*[A-Z]{5,6}[^A-Z]*$" .filename

Solution 2

Try with:

egrep '^[A-Z]{5,6}$'

where ^ and $ match beginning and ending of the line, respectively.

Share:
6,575

Related videos on Youtube

roger34
Author by

roger34

Updated on September 17, 2022

Comments

  • roger34
    roger34 over 1 year

    First off, this is homework and unfortunately I have to use egrep (and not with -w or anything of the like).

    I need my grep to return lines that have exactly 5 or 6 upper case letters.

    Doing egrep [A-Z]{5,6} .filename returns 5 and 6 letter words, but also unfortunately also more letter words than that.

    So I for example in this list:

    ASK
    roger ROBERT gulliver
    tom THOMAS
    JONATHAN moore
    MELISSA tenant
    

    I need it to return only ROBERT and THOMAS.

    • Zerg12
      Zerg12 about 13 years
      +1 all around, I wish I had more opportunity to use regexp's in my work life!
  • roger34
    roger34 about 13 years
    Forgot to mention that the words in the list were not at the start or end of the lines, my bad. Geeklab below had the right formula for the alternate version. Thanks for the help.
  • Geeklab
    Geeklab about 13 years
    Update: you are explicitly talking about no more than 7 uppercase letters. I'm not sure what you want with lowercase. My regexp does allow xROBERTx, while cYrus' regexp refuses it.