awk中具有搜索模式的逻辑操作?

时间:2019-04-19 09:03:40

标签: awk

如果我想使用awk打印包含optimization{architecture or policy or network}的任何行,应该怎么做?我尝试过

awk '/optimization & (architecture | policy | network)/{print}' file.txt

它不起作用。

3 个答案:

答案 0 :(得分:4)

您应该将&&运算符与两个正则表达式一起使用,并删除|运算符周围的空格:

awk '/optimization/ && /architecture|policy|network/' file

查看online awk demo

s="optimization and architecture 
optimization and policy 
optimization and network
optimization and awk
no words"
awk '/optimization/ && /architecture|policy|network/' <<< "$s"

输出:

optimization and architecture 
optimization and policy 
optimization and network

答案 1 :(得分:0)

@Wiktors answer对您的示例是正确的,但就像通常的其他FYI一样,您可以将regexp比较组合为(前2个等效):

  1. /re1/ && ((re2)|(re3))/
  2. /re1/ && (/re2/ || /re3/)
  3. (/re1/ && /re2/) || /re3/

请注意re2re31周围的括号,因为它们本身可能包含|

答案 2 :(得分:0)

通过以下任一方法尝试gnu awk

awk '/optimization/&&/architecture|policy|network/{print}' file.txt
awk '/optimization.*(architecture|policy|network)/{print}' file.txt