unix egrep not matching a certain group

Viewed 73

I have this textfile

6 times: 5 capitals, split by a space

EWTLE YQTCE FNTMA YVTMB GWTDH QGTAL
UVGEV SPGWP HDAVZ FLRVY HVBFT OFUSG
UKAYH BOAXR BLUSG YRMZT WAIMR BOCCX
BIUCZ KYUPP ECUZI PIURZ MXUMB RDUIG
ANAZW IVAYI QNHFN UPTHC YACTJ QPRLV 

Using Unix command line, egrep. I need to check if the middle character is the same in every group (here 4th line).

I tried to reverse the results if one of the letters was NOT the same, using a group. But I can't find how to do it.

egrep -v '[A-Z]{2}([A-Z])[A-Z]{2}.*[A-Z]{2}§NOT GROUP 1§[A-Z]{2}' filename

How can i fill in the part between §?

The solution i have is too long, just repeating the same for every group

egrep '[A-Z]{2}([A-Z])[A-Z]{2} [A-Z]{2}\1[A-Z]{2} 
[A-Z]{2}\1[A-Z]{2} [A-Z]{2}\1[A-Z]{2} [A-Z]{2}\1[A-Z]{2} [A-Z]{2}\1[A-Z]{2}' filename
2 Answers

This is slightly shorter and accomplishes the same thing:

[A-Z]{2}([A-Z])[A-Z]{2} ([A-Z]{2}\1[A-Z]{2} ?){5}

Test it here: https://www.regextester.com/

Using -P option (PCRE):

grep -P '^([A-Z]{2})([A-Z])(?1)(?: (?1)\2(?1)){5}$' file.txt

Output:

EWTLE YQTCE FNTMA YVTMB GWTDH QGTAL
BIUCZ KYUPP ECUZI PIURZ MXUMB RDUIG

Explanation:

^               # beginning of line
  ([A-Z]{2})    # group 1, 2 uppercases
  ([A-Z])       # group 2, 1 uppercase
  (?1)          # same pattern as group 1 (i.e. 2 uppercases)
  (?:           # start non capture group
                # 1 space
    (?1)        # same pattern as group 1 (i.e. 2 uppercases)
    \2          # same content as group 2, same letter
    (?1)        # same pattern as group 1 (i.e. 2 uppercases)
  ){5}          # end group, must appear 5 times
$               # end of line
Related