Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract data with field value greater than particular number

Tags:

linux

awk

I am trying to extract the values/methods which are taking more than particular milliseconds, I am unable to provide correct field separator

awk -F'=' '$3>960' file
awk -F'=||' '$3>960' file

This is a sample line:

logAlias=Overall|logDurationMillis=34|logTimeStart=2019-09-12_05:22:02.602|logTimeStop=2019-09-12_05:22:02.636|logTraceUID=43adbcaf55de|getMethod1=26|getMethod2=0|getMethod3=0|getMethod4=1|getMethod5=8

I do not see any result or i see it gives me all the transactions

like image 987
user3512605 Avatar asked Dec 11 '22 02:12

user3512605


2 Answers

Here is a generic, robust and easily extendible way:

awk -F'|' '{
  for(i=1;i<=NF;++i) {
    split($i,kv,"=")
    f[kv[1]]=kv[2]
  }
}
f["logDurationMillis"]>960' file
like image 194
oguz ismail Avatar answered Jan 30 '23 00:01

oguz ismail


You may use

awk -F'[=|]' '$4>960' file

Note that [=|] is a regex matching either = or | and the value you want to compare against appears in the fourth field.

See online demo:

s="logAlias=Overall|logDurationMillis=34|logTimeStart=2019-09-12_05:22:02.602|logTimeStop=2019-09-12_05:22:02.636|logTraceUID=43adbcaf55de|getMethod1=26|getMethod2=0|getMethod3=0|getMethod4=1|getMethod5=8
logAlias=Overall|logDurationMillis=980|logTimeStart=2019-09-12_05:22:02.602|logTimeStop=2019-09-12_05:22:02.636|logTraceUID=43adbcaf55de|getMethod1=26|getMethod2=0|getMethod3=0|getMethod4=1|getMethod5=8"
awk -F'[=|]' '$4>960' <<< "$s"

Output:

logAlias=Overall|logDurationMillis=980|logTimeStart=2019-09-12_05:22:02.602|logTimeStop=2019-09-12_05:22:02.636|logTraceUID=43adbcaf55de|getMethod1=26|getMethod2=0|getMethod3=0|getMethod4=1|getMethod5=8
like image 22
Wiktor Stribiżew Avatar answered Jan 30 '23 02:01

Wiktor Stribiżew