laitha0
laitha0

Reputation: 4336

regex pattern to find a line and then look for a number within the line

Hi I have some log files that logs operations from a software with their execution time, somewhere inside the log file I have lines that are similar to this one:

6/26/2013 6:56:43 AM -  >>> BuildStops.BuildStop.AddConsignee: 7198ms

If I do grep "BuildStops.BuildStop.AddConsignee:" System_2013626.log

I can get all the lines but I am trying to create a pattern that will allow me to find the lines that contain execution time over 10,000ms

Any help please

Upvotes: 1

Views: 182

Answers (7)

fedorqui
fedorqui

Reputation: 290325

Use the following:

grep -E "BuildStops.BuildStop.AddConsignee: [[:digit:]]{5}" file

it looks for 5 digits numbers, which is the pattern numbers >=10,000 accomplish.

Test

$ cat file
6/26/2013 6:56:43 AM - >>> BuildStops.BuildStop.AddConsignee: 7198ms
6/26/2013 6:56:43 AM - >>> BuildStops.BuildStop.AddConsignee: 17198ms
6/26/2013 6:56:43 AM - >>> BuildStops.BuildStop.AddConsignee: 171898ms
$ grep -E "BuildStops.BuildStop.AddConsignee: [[:digit:]]{5}" file
6/26/2013 6:56:43 AM - >>> BuildStops.BuildStop.AddConsignee: 17198ms
6/26/2013 6:56:43 AM - >>> BuildStops.BuildStop.AddConsignee: 171898ms

so I'd like it to be more dynamic, sometimes I want to look for over 10,000 sometime I want to look for over 4000

We can define 4000 as:
- [4-9] plus 3 digits.
- at least 5 digits.

$ cat file
6/26/2013 6:56:43 AM - >>> BuildStops.BuildStop.AddConsignee: 7198ms
6/26/2013 6:56:43 AM - >>> BuildStops.BuildStop.AddConsignee: 71998ms
6/26/2013 6:56:43 AM - >>> BuildStops.BuildStop.AddConsignee: 3198ms
6/26/2013 6:56:43 AM - >>> BuildStops.BuildStop.AddConsignee: 17198ms
6/26/2013 6:56:43 AM - >>> BuildStops.BuildStop.AddConsignee: 171898ms
12345
$ grep -E "BuildStops.BuildStop.AddConsignee: ([4-9][[:digit:]]{3}|[[:digit:]]{5})" file
6/26/2013 6:56:43 AM - >>> BuildStops.BuildStop.AddConsignee: 7198ms
6/26/2013 6:56:43 AM - >>> BuildStops.BuildStop.AddConsignee: 71998ms
6/26/2013 6:56:43 AM - >>> BuildStops.BuildStop.AddConsignee: 17198ms
6/26/2013 6:56:43 AM - >>> BuildStops.BuildStop.AddConsignee: 171898ms

Upvotes: 2

jaypal singh
jaypal singh

Reputation: 77175

awk solution:

awk '/BuildStops\.BuildStop\.AddConsignee:/ && $NF+0>10000' System_2013626.log
  • $NF+0 will evaluate execution time it in integer discarding the ms.

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336468

Easy:

grep -E "BuildStops\.BuildStop\.AddConsignee: ([5-9][0-9]{3}|[0-9]{5})" System_2013626.log

This matches the line only if (at least) five digits are present in that position, or a four-digit number that starts with at least 5. Also, don't forget to escape the literal dots.

Upvotes: 2

gbrener
gbrener

Reputation: 5835

grep "BuildStops\.BuildStop\.AddConsignee: \+[1-9][0-9]\{4,\}ms"

The \+ after the space matches "at least one" space. The \{4,\} after the [0-9] means "at least 4" digits between 0 and 9.

Upvotes: 0

mtotho
mtotho

Reputation: 52

How about

^[1-9]([0-9]){4}

Start with a digit 1-9, then 4 or more digits 0

So

"BuildStops.BuildStop.AddConsignee: [1-9]([0-9]){4}"

Upvotes: 0

Pu Gong
Pu Gong

Reputation: 176

this pattern can match 10000ms, 100000ms, and so on. It does not match 9999ms.

grep -E 'BuildStops.BuildStop.AddConsignee:[[:space:]]+[[:digit:]]{5,}ms'

Upvotes: 0

iruvar
iruvar

Reputation: 23394

Tested with GNU grep

grep -E "BuildStops.BuildStop.AddConsignee:.*[[:digit:]]{5,}ms"

Upvotes: 0

Related Questions