Find line then grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find line then grep
# 1  
Old 05-09-2016
Find line then grep

Hi,

I have a large log file which contains dates for specific events. Here's a sample of the log file:

Code:
Mon Sep 21 22:00:00 2015
VKRM started with pid=33, OS id=3652
Tue Sep 22 02:00:00 2015
Closing scheduler window
Closing Resource Manager plan via scheduler window
Clearing Resource Manager plan via parameter
Tue Sep 22 03:14:02 2015
Stopping background process CJQ0
Tue Sep 22 15:31:38 2015
Thread 1 cannot allocate new log, sequence 17
Private strand flush not complete
Current log# 16 seq# 16 mem# 0: /oracle/redologs/E2BE/logBE16.log
Thread 1 advanced to log sequence 17 (LGWR switch)
Current log# 1 seq# 17 mem# 0: /oracle/redologs/E2BE/logBE1.log
Tue Sep 22 21:04:28 2015
Thread 1 cannot allocate new log, sequence 18

Since an event might have occurred (since the log file do not rotate) more then once, the idea was to search for the present date, then grep the pattern.

For example, match "Tue Sep 22" by using date +%a" "%b" %d" then grep for the pattern.

Can you help me out? The language I'm trying to use is either perl or shell

Last edited by Corona688; 05-09-2016 at 12:40 PM..
# 2  
Old 05-09-2016
You're very close, but when using code tags, please use the code button, Image, not the icode one.

Find the date, then look for what pattern?
# 3  
Old 05-10-2016
Hi,

After finding "today"s date then look for this pattern:

"WARNING: aiowait timed out"

Note that this message was not included in the sample of the log I previously posted

Thanks
# 4  
Old 05-10-2016
Try:
Code:
sed -n "/$(date '+^%a %b %d')/,\$ s/WARNING: aiowait timed out/&/p"

# 5  
Old 05-11-2016
Hello Mr. Don
Please explain highlighted part
Code:
sed -n "/$(date '+^%a %b %d')/,\$ s/WARNING: aiowait timed out/&/p"

# 6  
Old 05-11-2016
The general form of the command:
Code:
sed -n "/$(date '+^%a %b %d')/,\$ s/WARNING: await timed out/&/p"

is:
Code:
sed -n "address1,address2 s/pattern/replacement/flags

In this case address1 is a basic regular expression (BRE) specifying a date that is to be matched as the starting point in a range of addresses and the literal dollar sign (\$) specifies the last line in the file as the ending point in the range of addresses to which the substitute command will be applied. The -n option to sed specifies that input lines will not be copied to the output unless there is an explicit request to do so in the commands executed. And, the p flag to the substitute command specifies that the input line is to be explicitly printed if and only if a substitution was made on that line. And, in this case a substitution is made if the string you are looking for is found on that line replacing the string you are looking for with itself (as specified by the & in the replacement string).
These 2 Users Gave Thanks to Don Cragun For This Post:
# 7  
Old 05-11-2016
Hi Don,

Excellent. This proved to work. sed is too powerful, but I'm still learning to use that.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep: Retrieve two strings from one file to find them anyone on line in another file

I am having trouble matching *two* strings from one file anywhere in a line of a second file, and could use some help getting this figured out. My preference would be to use grep for this because I would like to take advantage of its -A option. The latter is due to the fact that I would like both... (2 Replies)
Discussion started by: jvoot
2 Replies

2. Shell Programming and Scripting

Cut line from searched file if grep find neighbor columns

Hello All, While searching for the question, I found some answers but my implementation is not giving expected output. I have two files; one is sourcefile, other is named template. What I want to do is to search each line in template, when found all columns, cut the matching line from source... (4 Replies)
Discussion started by: baris35
4 Replies

3. SCO

Grep to ignore suffix & find end of line

In COBOL, a hyphen can be used in a field name and in a specific program some field names would be identical to others except a suffix was added--sometimes a suffix to a suffix was used. For example, assume I am looking for AAA, AAA-BBB, and AAA-BBB-CCC and don't want to look at AAA-BBB-CCC... (7 Replies)
Discussion started by: wbport
7 Replies

4. Shell Programming and Scripting

Using Grep & find & while read line in a script

Hello people! I would like to create one script following this stage I have one directory with 100 files File001 File002 ... File100 (This is the format of content of the 100 files) 2012/03/10 12:56:50:221875936 1292800448912 12345 0x00 0x04 0 then I have one... (0 Replies)
Discussion started by: Abv_mx81
0 Replies

5. UNIX for Dummies Questions & Answers

find/xargs/*grep: find multi-line empty "try-catch" blocks - eg, missing ; not in a commented block

How can I recursively find all files in a directory and print out the file and first line number of any text blocks that match the below cases? This would seem to involve find, xargs, *grep, regex, etc. In summary, I want to find so-called empty "try-catch blocks" that do not contain code... (0 Replies)
Discussion started by: lifechamp
0 Replies

6. Shell Programming and Scripting

How to use grep & find command to find references to a particular file

Hi all , I'm new to unix I have a checked project , there exists a file called xxx.config . now my task is to find all the files in the checked out project which references to this xxx.config file. how do i use grep or find command . (2 Replies)
Discussion started by: Gangam
2 Replies

7. Shell Programming and Scripting

Find a string using grep & print the line above or below that.

Hi All, Please tell me how can I Find a string using grep & print the line above or below that in solaris? Please share as I am unable to use grep -A or grep -B as it is not working on Solaris. (10 Replies)
Discussion started by: Zaib
10 Replies

8. Shell Programming and Scripting

grep to find entire line ....

As per my understanding below mentioned line of code finding a word 'boy' in $ACULOG... num_errors=`grep -i -e fail -e illegal -e exception -e "<E" -e boy $ACULOG | wc -l` if I'm not corerct, please correct me. How I can find entire line like "This is a boy" with something similar as above... (1 Reply)
Discussion started by: heyitsmeok
1 Replies

9. Shell Programming and Scripting

find out line number of matching string using grep

Hi all, I want to display line number for matching string in a file. can anyone please help me. I used grep -n "ABC" file so it displays 6 ABC. But i only want to have line number,i don't want that it should prefix matching context with line number. Actually my original... (10 Replies)
Discussion started by: sarbjit
10 Replies

10. Shell Programming and Scripting

grep string and find line before

hi, i have to grep for string in file but i want to find the group of this line so i must get lines before and select the group. the file look like : ####name_groupe1 alphanumeric line alphanumeric line .. ####name_groupe2 alphanumeric line alphanumeric line .. ####name_groupe3... (4 Replies)
Discussion started by: kamel.seg
4 Replies
Login or Register to Ask a Question