0

日志格式:

indexQuery.end, index=ssp_item_info, routing=188902615, total=0, nextScrollOffset=, logicTimeCost=3[ms]|

我想过滤一些逻辑时间成本高于 300 毫秒的日志,我该怎么做

4

3 回答 3

1

您能否尝试在 GNU 中使用显示的示例进行跟踪、编写和测试,我们检查条件的变量awk在哪里。thr=300

awk -v thr="300" '
match($0,/logicTimeCost=[0-9]+\[ms\]/){
  val=substr($0,RSTART,RLENGTH)
  gsub(/[^0-9]*/,"",val)
  if(val>thr){ print }
}
'  Input_file 
于 2021-01-20T07:51:10.597 回答
0

使用 GNU awk 您还可以使用范围从 300 到 300 的模式

awk '/logicTimeCost=([3-9][0-9]{2}|[1-9][0-9]{3,})\[ms\]/' file

范围匹配

  • [3-9][0-9]{2}匹配从 300 到 999
  • |或者
  • [1-9][0-9]{3,}从 1000 起匹配

或者使用 awk 的更长的版本

awk '/logicTimeCost=([3-9][0-9][0-9]|[1-9][0-9][0-9][0-9]+)\[ms\]/' file
于 2021-01-20T11:26:21.743 回答
-1

使用 GNU awk,您可以使用 match 命令来查找包含您要查找的字段的行,并将数字部分存储到一个数组中,您可以将其与您关心的值进行比较。

awk 'match($0, / logicTimeCost=([[:digit:]]+)/, m){ if(m[1]>300) print $0 }'

例如,这里有一些输入,其中的字段被打乱了。请注意,它会在该行中的任何位置找到该字段。

sauer@GRODD:~> cat /tmp/input 
indexQuery.end, index=ssp_item_info, routing=188902615, total=0, nextScrollOffset=, logicTimeCost=3[ms]|
indexQuery.end, index=ssp_item_info, routing=188902615, total=0, nextScrollOffset=, logicTimeCost=400[ms]|
indexQuery.end, index=ssp_item_info, routing=188902615, total=0, nextScrollOffset=, logicTimeCost=300[ms]|
indexQuery.end, index=ssp_item_info, logicTimeCost=301[ms], routing=188902615, total=0,1|
sauer@GRODD:~> awk 'match($0, / logicTimeCost=([[:digit:]]+)/, m){ if(m[1]>300) print $0 }' < /tmp/input 
indexQuery.end, index=ssp_item_info, routing=188902615, total=0, nextScrollOffset=, logicTimeCost=400[ms]|
indexQuery.end, index=ssp_item_info, logicTimeCost=301[ms], routing=188902615, total=0,1|

我比大多数其他答案更喜欢这个,因为它进行了正确的数字比较,而不是将数字与正则表达式进行比较。如果您稍后决定阈值是 278 或 306,那么使用正则表达式查找更大的值突然变得更加困难。不过,RavinderSingh13 的回答与此非常接近。

于 2021-01-20T22:48:52.797 回答