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:

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

You're very close, but when using code tags, please use the code button, , not the icode one.

Find the date, then look for what pattern?

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

Try:

sed -n "/$(date '+^%a %b %d')/,\$ s/WARNING: aiowait timed out/&/p"

Hello Mr. Don
Please explain highlighted part

sed -n "/$(date '+^%a %b %d')/,\$ s/WARNING: aiowait timed out/&/p"

The general form of the command:

sed -n "/$(date '+^%a %b %d')/,\$ s/WARNING: await timed out/&/p"

is:

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).

2 Likes

Hi Don,

Excellent. This proved to work. sed is too powerful, but I'm still learning to use that.