Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

showing the previous line to your search string 1

Status
Not open for further replies.

grimwyre

MIS
Jan 31, 2002
68
GB
I am attempting to script a check showing when an Oracle database has been started and stopped.

I can search the alertSID.ora file for the string "Starting" and get the output
"Starting ORACLE instance (normal)"

However the line I am really interested in is the previous line which shows the date e.g.

Sat May 25 11:26:35 2002
Starting ORACLE instance (normal)

Does anyone have any ideas on how to display the line previous to the one you have searched for or have a better idea on how to do this?
 
Try this:

sed -n '/Starting ORACLE instance /{g;1!p;};h' alertSID.ora

-jim
 
running the
sed -n '/Starting ORACLE instance /{g;1!p;};h' alertSID.ora
produces no output

running
sed '/Starting ORACLE instance /{g;1!p;};h' alertSID.ora
outputs all the lines in the file
 
Hmm... This worked when i tried it on Solaris. What Unix are you using? Maybe another version of sed.

The only other thing tha could be wrong is the search expression 'Starting ORACLE instance ' may not be matching anything in your file. Make sure the case is right.

It would be sed -n also.

-jim
 
This is on Solaris 8.

Running
sed -n '/Starting ORACLE instance /{g;1!p;};h' alertSID.ora

actually produces the correct number of blank lines. For example if there are two occurrences of the search string in the file two blank lines are produced but no actual data is shown.
 
Try

awk '/Starting ORACLE instance/{print a;print}{a=$0}' alertSID.ora CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
Why not approch it from a different view.

just put

echo "Oracle Stopped at `date`" > stop.log

in your shutdown script

and do the same for startup.

--
| Mike Nixon
| Unix Admin
| ----------------------------
 
Thanks.

The awk command works fine.

I have come across this problem several times in the past and not resolved it

Thanks again.
 
Another method, Cakiwi's is cleaner.
grep -B1 "Starting ORACLE instance" filename | sed -n '1p'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top