Solved

Help with GREP

Posted on 1998-07-17
4
425 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CPU monthly average? 12 150
How to Change Interface on Cisco IOU 11 125
spectrum scale snapshot  resotre/mount 1 58
check the file dates in unix 14 76
Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
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…
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 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…

737 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