使用awk和逻辑运算符过滤shell脚本中的多列

我必须过滤多个列:

1) Column 5 should have the word "Cat" or "Dog" 
2) Column 6 should be TRUE
3) Column 7 should be more than 7

我做了:

awk -F',' '$5~/Cat/ || $5~/Dog && $6=="TRUE" && $7>=7' cat.csv > catFinal.csv

我需要的是,在第一个条件的任何一部分成立之后,都应该应用下一个真正的条件。

评论
大三元
大三元

The simple solution is to group the first two tests for Cat or Dog into a single regular expression so your total conditional is a 3-component AND, e.g. test1 && test2 && test3. You can do that with:

 awk -F, '$5~/(Cat|Dog)/ && $6=="TRUE" && $7>=7' cat.csv > catFinal.csv

Which will ensure all aspects of the test are applied to each record. The conditionals are applied left-to-right, so as it was written, you would match any records where $5~/Cat/ or records where $5~/Dog && $6=="TRUE" && $7>=7 were true.

点赞
评论