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.
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.
This might work for you (GNU sed):
sed '/[^TAP]/d' file
If there in neither a T
or an A
or a P
delete the line.
or:
sed -n '/[^TAP]/!p' file
Turn of implicit printing and if the regexp is false, print the line.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.