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
awk print the next line on the current line ajp7701 Shell Programming and Scripting 5 05-25-2009 01:17 PM
awk to print current date? philipz UNIX for Dummies Questions & Answers 3 09-02-2008 04:58 PM
how to go previous line in vim useless79 UNIX for Advanced & Expert Users 2 09-16-2007 09:10 AM
How to print current month - 1 in string format mogli4 Shell Programming and Scripting 3 03-10-2006 04:47 PM
may be simple but i don't know -- Print current date from C program ls1429 High Level Programming 6 02-19-2002 01:50 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 10-16-2008
ysrinu ysrinu is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 15
Print previous, current and next line using sed

Hi,
how can i print the previous, current and next line using sed?
current line is the matching line.

The following prints all lines containing 'Failure' and also the immediate next line
cat $file | sed -n -e '/Failure/{N;p;}'

Now, i also want to print the previous line too.

Thanks,
-srinivas yelamanchili
  #2 (permalink)  
Old 10-16-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,807

Code:
sed -n -e '/Failure/{x;1!p;g;$!N;p;D;}' -e h  inputfilename

  #3 (permalink)  
Old 10-16-2008
ysrinu ysrinu is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 15
Thanks Jim, this worked.
-srinivas
  #4 (permalink)  
Old 10-16-2008
ysprathap ysprathap is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 4
Another option would be to use grep

grep -n -C1 Failure filename


-Prathap
  #5 (permalink)  
Old 10-16-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,345
If your grep version (GNU) supports the -A and -B option:


Code:
grep -A 1 -B 1 'Failure' file

Regards
  #6 (permalink)  
Old 10-17-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,092

Code:
sed -n '/pattern/ !{
h
}
/pattern/ {
N
H
x
p
}' filename

  #7 (permalink)  
Old 12-08-2008
ysrinu ysrinu is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 15
Thanks to all for the solution.
-A, B, C options are not supported.
Using HP-UX 11.11

Now, can i print just the previous line and the matching line?
cat replace_all* | sed -n -e 'N;/Failure/p;'
prints the previous line only for the first match, thereafter only matching lines are printed and not their previous lines.

Thanks,
-srinivas
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 02:05 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