amitmah amitmah - 1 year ago 92
Bash Question

grep command to find previous word less than a number

I need help with one grep command:

grep match-word tomcat-0.log.* | grep "TOMCAT BENCH" | grep -v Normal

The current output is simillar to:

tomcat-0.log:TOMCAT BENCH: match_word random-text 1420 elapsed Thu 2016-09-22 06:31:04:928 PDT <SessionID: id> <RequestID: reqId>

I want to modify this to display only those lines which have elapsed value greater than a number like

The word
is always present and the number is the word before

Can you please modify the
command to filter previous word and compare it with a number?

Answer Source

As asked for, here is an all grep solution in all it's brute force glory:

... | grep -E "(3000|3[0-9]{2}[1-9]|3[0-9][1-9]0|[4-9][0-9]{3}) elapsed"

What we do:

We match one of the following things:

  • 3000 matches the number 3000
  • or 3[0-9]{2}[1-9] matches all numbers 3001 - 3999 that do NOT end with a zero
  • or 3[0-9][1-9]0 matches numbers like 3010, 3120, 3990, etc. that end in zero but are not less than or equal to 3000
  • or [4-9][0-9]{3} matches numbers greater than 3999
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download