OS : RHEL 7.9
In somestrings.txt file (shown below), on the fourth field, I am trying to 'fetch' the lines that exactly matches the pattern w_sbc_q.
So, patterns like w_sbc_qkh_x, w_sbc_qkh_p seen below shouldn't be matched.
Hence I used the grep command shown under "This is what I tried" section.
My expected output:
Only Lines 3 and 4 from somestrings.txt should be returned.
$ cat somestrings.txt
paris:branch7:paris1:w_sbc_qkh_x:manhprd187.domain.net
paris:branch8:paris2:w_sbc_qkh_x:manhprd214.domain.net
madrid:branch13:madrid1:w_sbc_q:manhprd179.domain.net ## Line 3
madrid:branch14:madrid2:w_sbc_q:manhprd182.domain.net ## Line 4
madrid:branch13:madrid1:w_sbc_qkh_p:manhprd181.domain.net
madrid:branch14:madrid2:w_sbc_qkh_p:manhprd137.domain.net
This is what I tried. But, no luck. I want the below grep command to match only lines with w_sbc_q in the 4th field. But, my below command returns lines with w_sbc_qkh_x and w_sbc_qkh_p too.
$
$
$ MYPATTERN=w_sbc_q
$ grep "^[^:]*:[^:]*:[^:]*:[^:]*${MYPATTERN}" somestrings.txt
paris:branch7:paris1:w_sbc_qkh_x:manhprd187.domain.net
paris:branch8:paris2:w_sbc_qkh_x:manhprd214.domain.net
madrid:branch13:madrid1:w_sbc_q:manhprd179.domain.net
madrid:branch14:madrid2:w_sbc_q:manhprd182.domain.net
madrid:branch13:madrid1:w_sbc_qdh_p:manhprd181.domain.net
madrid:branch14:madrid2:w_sbc_qdh_p:manhprd137.domain.net
$
## Tried enclosing ${MYPATTERN} in single quotes. No output
$ grep "^[^:]*:[^:]*:[^:]*:[^:]*'${MYPATTERN}'" somestrings.txt
$
## Tried enclosing *${MYPATTERN} in single quotes. No output
$ grep "^[^:]*:[^:]*:[^:]*:[^:]'*${MYPATTERN}'" somestrings.txt
## Tried removing the asterix. No output (understandably)
$ grep "^[^:]*:[^:]*:[^:]*:[^:]${MYPATTERN}" somestrings.txt