Solved

Help with GREP

Posted on 1998-07-17
4
422 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
ID: 2008858
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
ID: 2008859
grep -B 1 TIME

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

0
 
LVL 1

Author Comment

by:hagenj
ID: 2008860
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
ID: 2008861
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.:

932 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

10 Experts available now in Live!

Get 1:1 Help Now