group characters within square brackets in grep

I have a file named zone-file with following content:

c3-Tx-Msi-US         IN      CNAME   c3-L123-Tx-Msi-US
c3-L123-Tx-Msi-US  IN      A       192.0.2.11
c5-Ny-Msi-US         IN      CNAME   c5-L123-Ny-Msi-US
c5-L123-Ny-Msi-US  IN      A       192.0.2.33
r6-Ms-Msi-UK         IN      CNAME   r6-L123-Ms-Msi-UK
r6-L123-Ms-Msi-UK  IN      A       192.0.2.16

I would like to grep out c3-Tx-Msi-US, c5-Ny-Msi-US and r6-Ms-Msi-UK, i.e. output of the grep command should be following:

c3-Tx-Msi-US
c5-Ny-Msi-US
r6-Ms-Msi-UK

I would like to accomplish this with as loose regular expression as possible which means that search for any character except space or tab up to string Msi and then again one or more characters except space or tab. So grep -Po "^[^ t]+Msi[^ t]+" zone-file would be perfect, but this also includes c3-L123-Tx-Msi-US, c5-L123-Ny-Msi-US and r6-L123-Ms-Msi-UK. Is it possible to group characters inside square brackets? I mean like grep -Po "^[^ t'L123']+Msi[^ t]+" zone-file? Looks like this is not possible?


Source: regex

Leave a Reply