need to grep contents of a file within specific time span. regex i am using is not working

Hi ,

I am trying to extract contents of a file between specified time stamp. but it does not seem to work. i am trying to extract output of /var/adm/messages between 15:00:00 to 15:23:59 .

i have tried two regex the first one seems to kind of work. it displays some output. the second one is complete blank. Please help me

First Regex with grep thats working :

grep 'May 29 15:[0-9][0-9]:[0-9][0-9]' /var/adm/messages

Second Regex with grep thats not working :

grep 'May 29 15:[0-9]{2}:[0-9]{2}' /var/adm/messages

why does the second one does not work ?

i am using solaris 10

That's an ERE (extended regular expression) you are using. So, you'll need to use the -E option with grep or use egrep.

A better regex (in your context) would be

'May 29 15:([01][0-9])|(2[0-3]):[0-5][0-9]'
grep 'May 30 05:[0-9]\{2\}:[0-9]\{2\}'

adapt to your dates/hours , of course .

The {} must be "escaped" with \ in this context

I don't think there is any need to escape the braces. The regex will work with the -E option without escaping.

The man's question (from what I understood ) was why his command doesn't provide the desired output and not what other command could provide the desired output.

Cheers!

I am not asking chidori to use a different command but the same command with an additional option with the regex untouched. By the way, have you tried your regex? I don't think it works.

The IREs don't require escaping in grep (unlike may be in sed).

i believe egrep uses ERE . but still its not working..

egrep 'May 29 15:[0-9]{2}:[0-9]{2}' /var/adm/messages

Try with

/usr/xpg4/bin/egrep
1 Like

try this :wink:

/usr/xpg4/bin/grep -E 'May 29 15:([01][0-9]|[2][0-3]):[0-9]'
1 Like

Try:

/usr/xpg4/bin/grep -E '^May 29 15:([01]|2[0-3])' /var/adm/messages
1 Like

/usr/xpg4/bin/grep -E 'May 29 15:[0-9]{2}:[0-9]{2}' /var/adm/messages

works :slight_smile:

but why not egrep 'May 29 15:[0-9]{2}:[0-9]{2}' /var/adm/messages ???

Please if some one has some details one the regex compatibility table with unix tools ( e.g. grep , grep , sed , awk , nawk and so ) that would be much helpful

There is no data between 15:00:00 and 15:23:59 on May 29 in that file..

-----
use /usr/xpg4/bin/grep -E or /usr/xpg4/bin/egrep on Solaris

you are right!!

sorry scurtinizer and ygemici , just now tried your codes on another file and voila!! it worked :slight_smile:

SUNWcsu is a core solaris package and grep an egrep and the others (/usr/bin/..) packages belong to that.

SUNWxcu4 utilities are like an enchanced the core utilities with XCU4(i m not sure but is is extendend core utilities) specifications..
These commands are an XPG4(X/Open Portability Guide) superset of Posix standarts from previous superset (XPG3)..
These packages are improved by the X/Open Company, Ltd and adapted to Sun (Sparc and x86 for Sun) platforms by the Oracle (Sun Microsystems ).
so the other name is X/Open Commands & Utilities..
and XCU4 utulities are placed in the "/usr/xpg4/bin" folder for prevent the conicts with existing Solaris behavior and these are suitable the POSIX.2 standarts.