The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
SED Search Pattern and Replace with the Pattern racbern Shell Programming and Scripting 4 03-15-2008 05:59 AM
Search Mulitiple String pattern in a file krishnan_6015@y Shell Programming and Scripting 2 11-23-2007 04:03 AM
Reading lines in a file matching a pattern torenji Shell Programming and Scripting 4 10-25-2007 04:15 AM
File search for pattern - script rahulrathod Shell Programming and Scripting 3 02-16-2007 04:03 AM
Search a file for pattern? davjoyce UNIX for Dummies Questions & Answers 2 03-07-2006 06:07 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-22-2005
frustrated1 frustrated1 is offline
Registered User
  
 

Join Date: Aug 2003
Location: Ireland
Posts: 278
Search file for pattern and grab some lines before pattern

I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in)

This is on solaris

Can you help?
  #2 (permalink)  
Old 12-22-2005
Sabari Nath S Sabari Nath S is offline
Registered User
  
 

Join Date: Jul 2005
Location: Chennai
Posts: 46
There is some option for grep itself for this same purpose like grep -An (for getting n lines above the searched pattern) and grep -Bn (for getting n lines below the searched pattern) ... not sure if this is there for solaris ... i think no ...

meanwhile u can try out this solution as well ...

grep -n pattern filename | cut -d":" -f1 | xargs -i expr {} - 2 | xargs -i sed -n '{},/pattern/ p' filename
  #3 (permalink)  
Old 12-22-2005
futurelet futurelet is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 137
Awk:
Code:
NR>3 { delete a[NR-3] }
/find this/ { print a[NR-2]; print a[NR-1]; print }
{ a[NR] = $0 }
Sponsored Links
Closed Thread

Bookmarks

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 01:16 AM.


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