|
Last Friday night, we had a database bounce...and bounce...and
bounce...61 times in three hours. I wanted to establish a
timeline for the restarts, hoping to see a pattern, but the
thought of manually going through the alert log just seemed too
mind numbing to undertake. It seemed a perfect task for sed,
the stream editor. Much of my sed information comes from an
excellent document named Useful One-Line Scripts for sed,
available at
http://sed.sourceforge.net/sed1line.txt. This document
provides no description of how commands work, but simply lists
dozens of examples. In my case, I knew what I wanted to do. I
wanted to scan the Oracle alert log, find the text Starting
ORACLE, and print the preceding line, which is the timestamp
for when Oracle was started. The following example for that
task, find a string and print the preceding line, is included in
the sed document:
# print the line
immediately before a regexp, but not the line
# containing the regexp
sed -n '/regexp/{g;1!p;};h'
Discouragingly, this command did not work. My command was
sed -n '/Starting
Oracle/{g;1!p;};h' < alertSID.log
The output was 61 blank lines. A little
research resulted in this script, which gave the correct
result.
sed -n -e
'/Starting ORACLE/{g;1!p;}' -e h < alertSID.log
sed reads the file from top to bottom. Every line is read
into what is called the pattern area. If the pattern area contains the text
Starting ORACLE, the value of a buffer area called
the holding area, which
contains the previous line, is moved to the pattern area. Then
the pattern area is printed. Finally for every line, the pattern
area is copied to the holding area, saving the current line. The individual arguments of
the script are:
- -n - Only print lines that are selected, meaning
a p command is executed. By default, sed prints all
lines.
- -e - The next argument is an editing command.
This is necessary because I have two edit command arguments,
delimited by the two
-e commands, that must operate independently, which I
believe is the problem with the script from the document I
was using.
- /Starting ORACLE/ - Select only those lines
containing the string Starting ORACLE.
- g - Move the holding area value to the pattern
area.
- 1!p - If the selected line is any line except the
first, print the value in the pattern area.
- -e - New editing command, operating on every line
independent from the previous commands.
- h - Move the value in the pattern area, which
will be the text sed has just read / processed, to the holding
area.
And the output looks like this:
Fri
May 19 19:08:02 2006
Fri
May 19 19:10:35 2006
... (lines not shown)
Fri May 19 21:35:33 2006
Fri May 19 21:53:36 2006
Note: This tip was tested using
Solaris 9.
Was this tip useful? Did you find any errors? Do you have any suggestions? Do you care? Click
here for the tip feedback page. Thank you.
|