简体   繁体   English

使用awk进行字符串匹配

[英]string match using awk

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 我想编写一个awk程序/ oneliner,为我提供行

field3 ==2 and field4 contains either "add" or "remove" field3 ==2并且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' 我可以使用正确的第一步: cat test.tsv | awk -F '\\t' '$3 == 2' cat test.tsv | awk -F '\\t' '$3 == 2'

How do I match the substrings for the second part? 如何匹配第二部分的子字符串? Thanks in advance 提前致谢

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM