Solved

Help with GREP

Posted on 1998-07-17
4
421 Views
Last Modified: 2010-04-21
I would like to GREP a file, when I find the word I am looking for I would like to retrieve that line and the line above it
i.e. If I was to GREP for TIME, the command would return

Mon Jun 01 15:59:04 1998
This is the TIME

I am using IRIX Unix
0
Comment
Question by:hagenj
4 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Depending on your version of grep,
 grep -B1 TIME
may work.  Otherwise, you might try something like
 head -`grep -n TIME file | head -1 | awk -F: '{print $1}'` file | tail -2
0
 
LVL 2

Expert Comment

by:JYoungman
Comment Utility
grep -B 1 TIME

Does what you are asking for (at least with GNU grep).

0
 
LVL 1

Author Comment

by:hagenj
Comment Utility
What I am tring to do is search a log file for errors starting with "ORA-", when I find one of this errors I want to read the line above the error because this line contains the time when the error happens. The time command in unix is returning the time the grep command finished, not when the "ORA-" time.
0
 

Accepted Solution

by:
answerman earned 50 total points
Comment Utility
The "standard" grep cannot do what you want. There is another "version" of grep called cgrep (which I've heard stands for context-grep -- give the context of what you grepped for)

Here's the URL for a perl script that does the same thing

http://www.cs.ruu.nl/pub/PERL/CPAN/scripts/nutshell/ch6/cgrep
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now