The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to negate grep result? mmdawg Shell Programming and Scripting 4 05-05-2008 09:24 AM
diaplaying the grep result rag84dec Shell Programming and Scripting 1 03-27-2008 02:37 AM
Outputting formatted Result log file from old 30000 lines result log<help required> vikas.iet Shell Programming and Scripting 5 12-02-2007 10:43 PM
egrep counting every 2 lines of result as 1 Orbix UNIX for Dummies Questions & Answers 1 11-20-2007 08:12 PM
How can you show lines surrounding a search string? robster UNIX for Dummies Questions & Answers 3 06-07-2005 12:33 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-19-2008
JamesByars JamesByars is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 124
how to get surrounding lines of grep result

hi,

if i have a file and i want to search for the word error using grep, i usually want to see the surrounding lines too as they contain info about the error. what would be a nice way to achieve this?

thanks
  #2 (permalink)  
Old 07-19-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,342
Use the -A and -B option of grep.

Regards
  #3 (permalink)  
Old 07-20-2008
JamesByars JamesByars is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 124
oops, I missed those. thanks
  #4 (permalink)  
Old 07-22-2008
JamesByars JamesByars is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 124
Hi,

my grep on HP-UX does not have this -A and -B option

my grep is HP-UX 11i Version 2: August 2003

Is there any other way to achieve what I want to do?

thanks
  #5 (permalink)  
Old 07-22-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Cool without seeing an example

sometimes you can


Code:
sed "s/good data/good data+/"
tr "\n" "~"
tr "+" "\n"
grep for what you need
tr "~" "\n"

Assuming the end of previous entry is with "good data". I put a plus sign there so I can later force the new-line back.
grep for what I need, and pipe the output through another tr to get my new-line characters back.
  #6 (permalink)  
Old 07-22-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 717
Hi.

There are awk and perl solutions in the thread grep and display few lines before and after

I tried them both and they work. The perl solution is far longer, but more general.

An interesting and powerful utility is found at cgrep home page referred to from freshmeat.net: Project details for cgrep but will require some work to configure and compile the c code of the project:
Quote:
DESCRIPTION
cgrep provides all the features of grep, egrep, and fgrep, with greatly
enhanced performance (see the section on PERFORMANCE) along with many
additional features, one of which is the ability to output the context
(surrounding lines) of the matching lines. The use of cgrep is upward-
compatible with that of grep, egrep (using the -E option), or fgrep
(using the -F option).

-- excerpt from man cgrep
... cheers, drl
  #7 (permalink)  
Old 07-23-2008
JamesByars JamesByars is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 124
thanks all
Closed Thread

Bookmarks

Tags
solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:48 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0