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
grepping columns ScKaSx UNIX for Dummies Questions & Answers 8 02-29-2008 06:44 PM
Grepping issue.. LinuxRacr Shell Programming and Scripting 9 02-28-2008 12:26 PM
Grepping for strings t4st33@mac.com UNIX for Dummies Questions & Answers 5 06-20-2007 11:51 PM
grepping for a sentence eloquent99 UNIX for Dummies Questions & Answers 2 04-23-2003 02:40 PM
grepping eloquent99 UNIX for Dummies Questions & Answers 5 04-22-2003 10:20 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 08-24-2007
cbeauty cbeauty is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 2
grepping around

Using shell scripts, I use grep to find the word “error” in a log file:
grep error this.log.
How can I print or get the line 3 lines below the line that word “error” is located?
Thanks in advance for your response.
  #2 (permalink)  
Old 08-24-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Quote:
Originally Posted by cbeauty View Post
Using shell scripts, I use grep to find the word “error” in a log file:
grep error this.log.
How can I print or get the line 3 lines below the line that word “error” is located?
Thanks in advance for your response.
Code:
awk 'x&&!--x;/error/{x=3}' file
Use nawk or /usr/xpg4/bin/awk on Solaris.

With sed:

Code:
sed -n '/error/{n;n;n;p;}' file
With shell:

Code:
while read;do case $REPLY in *error*)read;read;read;echo "$REPLY";;esac;done<file

Last edited by radoulov; 08-24-2007 at 02:59 PM.. Reason: sed added
  #3 (permalink)  
Old 08-27-2007
manas_ranjan's Avatar
manas_ranjan manas_ranjan is offline
Registered User
  
 

Join Date: Jul 2007
Location: Amsterdam
Posts: 177
hey please try the following one


grep -A <<n>> <<search pattern>> file_name
grep -B <<n>> <<search pattern>> file_name



NOTE: n is the number of lines you want to traverse before/after the search pattern .
  #4 (permalink)  
Old 08-27-2007
cbeauty cbeauty is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 2
radoulov,
You are very knowledgeable.
nawk 'x&&!--x;/error/{x=3}' file
is what I needed.
Thank you.
  #5 (permalink)  
Old 08-27-2007
LaLonde LaLonde is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 4
Hi. I am a new user to this site. This is actually my first post.

My question is similar. How can I grep for a particular string and have grep return that string along with the next 10 lines beneath the grepped string? manas_ranjan said to issue grep -A <<n>> <<search pattern>> file_name, but it doesn't look like -A is a grep option on the Solaris 10 box I am trying to run the command on. I assume I should install the latest version of GNU grep, but I am not an admin of this box, so that is not an option.

Are there alternatives such as awk/nawk or sed? If so, what would the exact commands be?

Thanks you very much in advance.

- LaLonde
  #6 (permalink)  
Old 08-28-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Code:
awk 'c&&c--;/pattern/{c=10;print}' file
Use nawk or /usr/xpg4/bin/awk on Solaris.

Last edited by radoulov; 08-28-2007 at 02:01 AM.. Reason: correction
  #7 (permalink)  
Old 08-28-2007
ahmedwaseem2000 ahmedwaseem2000 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Bangalore
Posts: 219
refer to radoulov's post he has given the example.

heres another alternative

Code:
 sed -n '/1/{n;p;n;p;n;p;n;p;n;p;n;p;n;p;n;p;n;p;n;p;}' test
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 09:58 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