user3512605
user3512605

Reputation: 67

How to extract data with field value greater than particular number

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

Upvotes: 4

Views: 145

Answers (4)

Jotne
Jotne

Reputation: 41456

Here is how I do it:

awk -F'logDurationMillis=' '{split($2,a,"[|]")} a[1]>960' file

If its log duration logDurationMillis you are looking for, I do set it as a line separator. This way I know for sure the next data is the value to get. Then split the next data by | to get the number in front of it. Then a[1] have your value and you can test it against what you need. No loop, so should be fast.

Upvotes: 2

oguz ismail
oguz ismail

Reputation: 50750

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

Upvotes: 6

RavinderSingh13
RavinderSingh13

Reputation: 133458

Could you please try following, this may help you in case your string is not coming in a fixed place.

awk '
match($0,/logDurationMillis=[0-9]+/){
  if(substr($0,RSTART+18,RLENGTH-18)+0>960){
    print
  }
}
'  Input_file

2nd Solution:

awk '
match($0,/logDurationMillis=[0-9]+/){
  val=substr($0,RSTART,RLENGTH)
  sub(/.*=/,"",val)
  if(val+0>960){
    print
  }
}
'  Input_file

Upvotes: 2

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626748

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

Upvotes: 4

Related Questions