?
Solved

Help with GREP

Posted on 1998-07-17
4
Medium Priority
?
427 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses
Course of the Month9 days, 12 hours left to enroll

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