Suppose I want to find a regular expression of this form:
awk '!/[^TAP]/ {print $1}' sample.txt
which gives matches for ATA, but not for ATAU, TAR and TR.
On the other hand this gives matches for all four of them:
awk '/[TAP]/ {print $1}' sample.txt
What is the correct regex syntax in grep
and sed
for the former case?
Mock input file is:
ATA
ATAU
TAR
TR
Output should be the line composed by a string containing only characters A and/or T and/or P, but not any other character.
CodePudding user response:
You may use this awk
:
awk '/^[ATP] $/' file
ATA
Or this grep
would also work:
grep '^[ATP]\ $' file
ATA
Regex pattern ^[ATP] $
will match one or more of A
, T
or P
letters and due to anchors ^
and $
, it won't allow any other character.