Grep unix time range gives invalid range error

I need to grep this time range: 2020-04-27T20:29:27 - 2020-04-27T20:3x (any time after 30)

However this grep gives me an invalid time range error:

egrep MESSAGE_SENT company-runtime-cdrs*.log | grep '27T19:[29-30]' | wc -l
grep: Invalid range end
0

How can I do this correctly?

2

1 Answer

I suppose you are trying to match minutes 29 and 30.

why your expression does not work

[29-30] does not do what you want from it; it means

2 or (any character from the range from 9 to 3) or 0.

The range is indeed invalid because 3 comes before 9 (*).


how to do it

To achieve what you want (i.e. matching 29 and 30), you can simply use (29|30) in your regex, like

grep MESSAGE_SENT company-runtime-cdrs*.log | grep '27T20:\(29\|30\)'

or

egrep MESSAGE_SENT company-runtime-cdrs*.log | egrep '27T20:(29|30)'

(note that egrep needs less escaping so it is more readable)

your exact time range

I need to grep this time range: 2020-04-27T20:29:27 - 2020-04-27T20:3x (any time after 30)

this is a bit more complicated:

grep MESSAGE_SENT company-runtime-cdrs*.log | grep '27T20:\(29:\(2[7-9]\|[3-5].\)\|3.:\)'

or

egrep MESSAGE_SENT company-runtime-cdrs*.log | egrep '27T20:(29:(2[7-9]|[3-5].)|3.:..)'

further advice

I suggest to use a tool like regexr to test / explain your regexen. I used it too to check what I came up with above.

(*) formally this is locale-dependent, but I have yet to see a locale where it is not true

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like