bash - egrep for time in H:MM:SS format

08
2014-07
  • Serg12

    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?

  • Answers
  • Azz

    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)'
    
  • erik

    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.


  • Related Question

    linux - Using egrep to find lines that have 5-6 Upper Case letters. (but not more than 6)
  • roger34

    First off, this is homework and unfortunately I have to use egrep (and not with -w or anything of the like).

    I need my grep to return lines that have exactly 5 or 6 upper case letters.

    Doing egrep [A-Z]{5,6} .filename returns 5 and 6 letter words, but also unfortunately also more letter words than that.

    So I for example in this list:

    ASK
    
    roger ROBERT gulliver
    
    tom THOMAS
    
    JONATHAN moore
    
    MELISSA tenant
    

    I need it to return only ROBERT and THOMAS.


  • Related Answers
  • Geeklab
    egrep "^[^A-Z]*[A-Z]{5,6}[^A-Z]*$" .filename
    
  • cYrus

    Try with:

    egrep '^[A-Z]{5,6}$'
    

    where ^ and $ match beginning and ending of the line, respectively.

  • Rolnik

    Yet another version

    a) limited to 5 or 6 Upper Cases in the word;

    b) word can have unlimited lower case, plus the '-' and the apostrophe: '

    c) uses the \b word edge character (zero-width);

    This finds more than your list, (which you would want), and follows all limitations you state. This may result in more than you bargained for, since it also permits any number of lower-case letters between the upper-case letters:

    "\b[a-z'-]([A-Z][a-z'-]){5,6}\b"

    Will find: awesome JET-LI

    Jane DeSILVA

    But not: old MacDONALD