0

Working on the file an need to grep the line with a time stamp in the H:MM:SS format. I tried the following egrep '[0-9]:[0-9]:[0-9]'. Didn't work for me. What am i doing wrong in regex?

Serg12
  • 101
  • 1

3 Answers3

3

if you want to match for
00:00:00 to 23:59:59

egrep '([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]'t

if you want to match for
12:00 pm to 12:59 am

egrep -i '((?:0[0-9]|1[012]):[0-5][0-9]:[0-5][0-9]\s?(?:a|p)m)'
Sirius_Black
  • 203
  • 1
  • 2
  • 7
0

[0-9]:[0-9]:[0-9] would match H:M:S, bot not H:MM:SS.

Each set of square brackets matches a single character.

So to get something that matches H:MM:SS you could use:

[0-9]:[0-9][0-9]:[0-9][0-9]

Note, the above will NOT match H:M:S

In order to match H:MM:SS OR H:M:S OR H:M:SS OR H:MM:S use the following

[0-9]:[0-9]+:[0-9]+

+ means match the last character, 1 or more times (including the first match)

Azz
  • 4,815
  • 2
  • 28
  • 23
0

I suggest you use

egrep '[0-9]{2}:[0-9]{2}:[0-9]{2}'

where {2} means match last character at least two times and maximum two times (= exactly two times), where last character in this example was a number between 0 and 9.

If you want to search for it least one and maximum two you have to change it to

egrep '[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}'

which matches for example on 0:1:22 and 0:11:22 and 00:11:22 and 00:11:2 and so on.

The answer Sirius_Black gave is also testing on real times. My answer would match for unrealistic times like 99:99:99 – which could however be a time duration.

erik
  • 2,028