简体   繁体   English

在多个字段中awk多个字符串

[英]awk multiple strings in multiple fields

I have a file 我有一个档案

file.txt file.txt

0005663;2018-04-24 10:14:58;Apple;2018-04-24 10:34:10;Banana;2018-04-24 15:03:16;Grape;2018-04-24 17:13:17;Grape
0005664;2018-04-24 10:14:58;Apple;2018-04-24 10:34:10;Avocado;2018-04-24 15:03:16;Orange;2018-04-24 17:13:17;Orange
0005665;2018-04-24 10:14:58;Apple;2018-04-24 10:34:10;Cherry;2018-04-24 15:03:16;Lemon;2018-04-24 15:14:10;Apple;2018-04-24 15:41:10;Orange;2018-04-24 17:13:17;Orange
0005666;2018-04-24 10:14:58;Apple;2018-04-24 10:34:10;Banana;2018-04-24 15:03:16;Melon;2018-04-24 16:13:11;Grape;2018-04-24 17:13:17;Grape
0005667;2018-04-24 10:14:58;Apple;2018-04-24 10:34:10;Melon;2018-04-24 15:03:16;Grape;2018-04-24 17:13:17;Grape
0005668;2018-04-24 10:14:58;Apple;2018-04-24 10:34:10;Cherry;2018-04-24 15:03:16;Grape;2018-04-24 16:13:11;Grape;2018-04-24 17:13:17;Papaya

My log has several fields, depending on which path the client made. 我的日志有几个字段,具体取决于客户端选择的路径。 It can have 7, or 8, 9 fields, as can be 14 or 16 ... I need to get the lines that had this route: 它可以有7或8、9个字段,可以有14或16个字段...我需要获取具有以下路线的行:

  • in the third field = Apple 在第三个字段中=苹果
  • in the fifth field = Banana, Avocado or Cherry 在第五个字段中=香蕉,鳄梨或樱桃
  • and the seventh field ahead = Apple, Grape, Orange or Lemon and not be Melon or Papaya 前面的第七场=苹果,葡萄,橙子或柠檬,而不是瓜子或木瓜

To make this route to the seventh field I do so 为了使这条路线到达第七场,我这样做

awk -F";" '($3~/Apple/) && ($5~/Banana/ || $5~/Avocado/ || $5~/Cherry/) && ($7~/Apple/ || $7~/Grape/ || $7~/Orange/ || $7~/Lemon/) &&! ($7~/Melon/ || $7~/Papaya/)' file.txt

how to do with ninth field or eleventh ... without having to write a rule for each lenght? 如何处理第九个字段或第十一个字段...而不必为每个长度编写规则?

the output would look like this: 输出如下所示:

0005663;2018-04-24 10:14:58;Apple;2018-04-24 10:34:10;Banana;2018-04-24 15:03:16;Grape;2018-04-24 17:13:17;Grape
0005664;2018-04-24 10:14:58;Apple;2018-04-24 10:34:10;Avocado;2018-04-24 15:03:16;Orange;2018-04-24 17:13:17;Orange
0005665;2018-04-24 10:14:58;Apple;2018-04-24 10:34:10;Cherry;2018-04-24 15:03:16;Lemon;2018-04-24 15:14:10;Apple;2018-04-24 15:41:10;Orange;2018-04-24 17:13:17;Orange

Could you please try following and let me know if this helps you. 您能否尝试遵循并让我知道这是否对您有帮助。

awk -F";" '
($3=="Apple" && ($5=="Banana" || $5=="Avocado" || $5=="Cherry")){
  for(i=6;i<=NF;i++){
    if($i ~ /Apple|Grape|Orange|Lemon/){  flag=1    }
    if($i ~ /Melon|Papaya/)            {  non_flag=1}
  }
  if(!non_flag && flag)                {  print     }
  non_flag=flag=""
}'  Input_file

If I am reading it right OP is saying greater than field 5 Papaya and Melon shouldn't come 如果我没看错的话,OP说的要比第5场大,木瓜和瓜不应该来

and in fields greater than 5 = Apple, Grape, Orange or Lemon and not be Melon or Papaya 且大于5的字段=苹果,葡萄,橙或柠檬,而不是瓜或木瓜

So keeping that in mind have written this. 因此请牢记这一点。

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

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