Reputation: 5041
I have a tab separated file that has rows like this:
field1 field2 field3 field4 field5 field6
1 abc 2 word:add,word:remove text string
2 xyz 2 word:replace,word:modify msg string
3 lmn 1 word:add msg numeric
4 cncn 2 phone:add,phone: remove msg numeric
5 lmn 2 word:add msg text
I want to write an awk program/oneliner that gives me the rows where
field3 ==2
and field4 contains either "add" or "remove"
In other words it should have first filtered out these and,
1 abc 2 word:add,word:remove text string
2 xyz 2 word:replace,word:modify msg string
4 cncn 2 phone:add,phone:remove msg numeric
5 lmn 2 word:add msg text
In the second step should have filtered out these
1 abc 2 word:add,word:remove text string
4 cncn 2 phone:add,phone:remove msg numeric
5 lmn 2 word:add msg text
I can get the first step right using : cat test.tsv | awk -F '\t' '$3 == 2'
How do I match the substrings for the second part? Thanks in advance
Upvotes: 0
Views: 101
Reputation: 123448
You could match the field using ~
:
awk -F '\t' '$3==2 && $4 ~ /add|remove/' filename
would produce the desired result:
1 abc 2 word:add,word:remove text string
4 cncn 2 phone:add,phone: remove msg numeric
5 lmn 2 word:add msg text
Quoting from the manual:
~ !~ Regular expression match, negated match. NOTE: Do not use
a constant regular expression (/foo/) on the left-hand side
of a ~ or !~. Only use one on the right-hand side. The
expression /foo/ ~ exp has the same meaning as (($0 ~
/foo/) ~ exp). This is usually not what was intended.
Upvotes: 3