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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need to grep contents of a file within specific time span. regex i am using is not working
# 1  
Old 05-30-2012
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 :

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


Second Regex with grep thats not working :

Code:
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
# 2  
Old 05-30-2012
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

Code:
'May 29 15:([01][0-9])|(2[0-3]):[0-5][0-9]'

# 3  
Old 05-30-2012
Code:
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

Last edited by Franklin52; 05-30-2012 at 08:26 AM.. Reason: Please use code tags
# 4  
Old 05-30-2012
Quote:
Originally Posted by black_fender
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.
# 5  
Old 05-30-2012
Quote:
Originally Posted by elixir_sinari
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!
# 6  
Old 05-30-2012
Quote:
Originally Posted by black_fender
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.
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).
# 7  
Old 05-30-2012
i believe egrep uses ERE . but still its not working..

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep a log file starting from a specific time to the end of file

I have a log file which have a date and time at the start of every line. I need to search the log file starting from a specific time to the end of file. For example: Starting point: July 29 2018 21:00:00 End point : end of file My concern is what if the pattern of `July 29 2018 21:00:00`... (3 Replies)
Discussion started by: erin00
3 Replies

2. UNIX for Beginners Questions & Answers

Total size utilizes by the files older than a time span

Through find command I identified the files older that 1 year. I need the overall size utilizes by these 1 year older files. Please share me the command to identify it .Thanks Please post in an adequate technical forum! (3 Replies)
Discussion started by: Sang
3 Replies

3. Shell Programming and Scripting

awk to place specific contents filename within text file

I am trying to use awk to place the contens of a filename in $1 and $2 followed by the data in the text file. Basically, put the filename within the text file. There are over 1000 files in the directory and as of now each file is saved with a unique name but it is not within the file. Thank you... (10 Replies)
Discussion started by: cmccabe
10 Replies

4. UNIX for Dummies Questions & Answers

Looping/Reading file contents not working

Hi, I am doing something basic, but I am missing something. Im trying to read the contents of a file and taking those values and connecting to a database. However, it only connect to one (or reads in) value and then exists. Here is what it looks like: listname.txt db1 db2 db3 Script:... (15 Replies)
Discussion started by: DBnixUser
15 Replies

5. Shell Programming and Scripting

select the lines in between some time span

Hi Everyone ! i want to take all the lines from a file that falls in between some date... and every line in a file has a time stamp.. ---some text---- 01/Jan/2010 ---- some other text ---- ---some text---- 10/Jan/2010 ---- some other text ---- ---some text---- 20/Dec/2010 ---- some... (3 Replies)
Discussion started by: me_newbie
3 Replies

6. Shell Programming and Scripting

How to replace specific contents in a file?

From the existing file, I need to replace specific contents possibly with var every time when the user changes the var. e.g the contents in the file file.txt is 'My name is $n and I am $y years old' and every time user changed the var outside the file, the contents of the file should be created... (4 Replies)
Discussion started by: Emilywu
4 Replies

7. UNIX for Dummies Questions & Answers

Extracting a Time Span from Syslog Messages File

Hi all, I need your help to explain how I can extract a time span from the syslog messages file on a Solaris 10 system. Here is an example extract of the syslog messages: Dec 4 11:51:38 hajap141-0107.nls.jlrint.com 267938: Dec 4 11:51:36: %DOT11-6-DISASSOC: Interface Dot11Radio0,... (4 Replies)
Discussion started by: wthomas
4 Replies

8. Shell Programming and Scripting

Bash copy file contents into an existing file at a specific location

Hi all I need to copy the entire contents of one file into an existing file at a specific location. I know the exact line number where I need to put it. It appears I would use either sed or awk to do this, but I have been unsuccessful so far: File A line 1 line 2 line 3 line 4 ... (6 Replies)
Discussion started by: gshepherd7
6 Replies

9. Shell Programming and Scripting

awk - print file contents except regex

Hello, I have a file which has user information. Each user has 2 variables with the same name like Email: testuser1 Email: testuser1@test.com Email: testuser2 Email: testuser2@test.com My intention is to delete the ones without the '@' symbol. When I run this statement awk '/^Email:/&&!/@/'... (6 Replies)
Discussion started by: rmsagar
6 Replies

10. Shell Programming and Scripting

Reading specific contents from a file and appending it to another file

Hi, I need to write a shell script (ksh) to read contents starting at a specific location from one file and append the contents at specific location in another file. Please find below the contents of the source file that I need to read the contents from, File 1 -----# more... (5 Replies)
Discussion started by: dnicky
5 Replies
Login or Register to Ask a Question