Find the answer to your Linux question:
Results 1 to 2 of 2
Hi, I am learning regex and was wonder about parsing a time string using egrep. File test.txt contains: 09:23 PM 99:23 PM 9:23 PM 11:23 AM Command line: $> egrep ...
  1. #1
    Just Joined!
    Join Date
    Mar 2009
    Location
    Salt Lake City
    Posts
    1

    regex, string match question

    Hi,

    I am learning regex and was wonder about parsing a time string using egrep.

    File test.txt contains:
    09:23 PM
    99:23 PM
    9:23 PM
    11:23 AM

    Command line:
    $> egrep -i '(1[012]|0[1-9]|(\<[1-9])):[0-5][0-9] (am|pm)' test.txt

    1[012] gets 10 11 12
    0[1-9] matches 01-09
    \<[1-9] matches 1-9: // Eliminates the 99:23, not sure about this as a good solution

    Is there a better way to restrict the initial match to 1-9: or 01-09: or 10-12: ?

    Thanks in advance

    Pat

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    why make it so complicated. Use awk and set field delimiter to either space and semi colon
    Code:
    # awk 'BEGIN{FS=":|[ ]"} $1 <= 12 && $2 <=59 && $3 ~/AM|PM/' file
    09:23 PM
    9:23 PM
    11:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...