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
search excat string in another string (grep "fails") bora99 UNIX for Dummies Questions & Answers 0 06-05-2008 07:41 AM
GREP and Retrieve Line by Line maxmave Shell Programming and Scripting 2 06-03-2008 12:45 PM
grep string and find line before kamel.seg Shell Programming and Scripting 4 12-17-2007 12:48 PM
grep the string with the line number salaathi Shell Programming and Scripting 4 12-04-2007 11:42 AM
grep a string in a line using sed viadisky UNIX for Dummies Questions & Answers 4 02-06-2007 06:03 AM

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 05-23-2008
karthikn7974's Avatar
karthikn7974 karthikn7974 is offline VIP Member  
Supporter
  
 

Join Date: Jul 2007
Location: Singapore
Posts: 58
Grep string and next line

Hi
i am having a file of more than 60,000 lines.

need to grep a string and write the line of string and next line of the grepped string

any suggestions
  #2 (permalink)  
Old 05-23-2008
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 439
From man grep:


Code:
OPTIONS
       -A NUM, --after-context=NUM
              Print  NUM  lines  of  trailing  context  after  matching lines.
              Places  a  line  containing  --  between  contiguous  groups  of
              matches.

  #3 (permalink)  
Old 05-23-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,926
If you don't have GNU grep you could use sed:


Code:
sed -n '/pattern/{N;p;}' input

  #4 (permalink)  
Old 05-23-2008
penchal_boddu penchal_boddu is offline
Registered User
  
 

Join Date: Apr 2008
Location: Bangalore
Posts: 127
using awk,

awk ' $0 ~ /search_string/ { print ; t=1 ; next }
t==1{print;t=0}' file_name

Thanks
Penchal
  #5 (permalink)  
Old 05-23-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,926
Or:


Code:
awk '_&&_--;/pattern/{_=2}' input

Use nawk or /usr/xpg4/bin/awk on Solaris.
  #6 (permalink)  
Old 05-23-2008
karthikn7974's Avatar
karthikn7974 karthikn7974 is offline VIP Member  
Supporter
  
 

Join Date: Jul 2007
Location: Singapore
Posts: 58
Hi radoulov,

your command outputs 2 lines after the grepped string. How to get the grep string line and next line
  #7 (permalink)  
Old 05-23-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,926
Quote:
Originally Posted by karthikn7974 View Post
Hi radoulov,

your command outputs 2 lines after the grepped string. How to get the grep string line and next line
Oops, sorry, it should be:


Code:
awk '/pattern/{_=2}_&&_--' input


Last edited by radoulov; 05-23-2008 at 06:07 AM.. Reason: corrected
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 04:56 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