Grep unix time range gives invalid range error
Solution 1:
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 from9
to3
) or0
.
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