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
Matching fields of rows and then operating ashis.tewari Shell Programming and Scripting 3 12-04-2008 09:02 AM
sum of all matching rows using awk i.scientist UNIX for Dummies Questions & Answers 8 08-06-2008 09:13 PM
how to delete duplicate rows in a file vamshikrishnab Shell Programming and Scripting 5 06-18-2008 11:00 AM
How to delete particular rows from a file suresh3566 Shell Programming and Scripting 5 06-02-2008 06:07 AM
Delete repeated rows from a file tonet Shell Programming and Scripting 8 04-08-2008 10:42 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 03-19-2009
Juha Juha is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 36
SED: delete matching row and 4 next rows?

Hi,

Tried to look for solution, and found something similar but could not adapt the solution for my needs..

I'm trying to match a pattern (in this case "ProcessType")in a logfile, then delete that line and the 4 following lines.

The logfile looks as follows:

Code:
ProcessType:    PROCESS_A (0) <---- delete this
SequenceNumber: 8285 <---- delete this
 <---- delete this
2009 Mar 07  22:04:23:679   0:8285 <---- delete this
 <---- delete this
ProcessType:    PROCESS_A (0)
SequenceNumber: 8286

2009 Mar 07  22:04:23:679   0:8286

ProcessType:    PROCESS_B (68)
SequenceNumber: 40689

2009 Mar 07  22:04:23:698  68:40689

DATA that should not be deleted

ProcessType:    PROCESS_B (68)
SequenceNumber: 40690

2009 Mar 07  22:04:23:698  68:40690

DATA that should not be deleted

ProcessType:    PROCESS_C (93)
SequenceNumber: 36235

2009 Mar 07  22:04:23:829  93:36235

ProcessType:    PROCESS_C (93)
SequenceNumber: 36236

2009 Mar 07  22:04:23:829  93:36236

DATA that should not be deleted

I tried this:


Code:
sed -e '/\<ProcessType\>/,/$/d' < log.txt > test

But that only resulted in this:


Code:
2009 Mar 07  22:04:23:679   0:8285


2009 Mar 07  22:04:23:679   0:8286


2009 Mar 07  22:04:23:698  68:40689


2009 Mar 07  22:04:23:698  68:40690


2009 Mar 07  22:04:23:829  93:36235


2009 Mar 07  22:04:23:829  93:36236


2009 Mar 07  22:04:23:945  91:89062


2009 Mar 07  22:04:23:945  91:89063


2009 Mar 07  22:04:24:018  91:89064


2009 Mar 07  22:04:24:018  91:89065


2009 Mar 07  22:04:24:018  91:89066

  #2 (permalink)  
Old 03-19-2009
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,345
If awk is allowed:


Code:
awk '/ProcessType:/ && !p {i=-4;p=1} i++ > 0' file

Regards
  #3 (permalink)  
Old 03-19-2009
Juha Juha is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 36
Thanks franklin, did not work for me however..


Code:
# awk '/ProcessType:/ && !p {i=-4;p=1} i++ > 0' file
awk: syntax error near line 1
awk: bailing out near line 1

With nawk it executes but only deletes the first occurrence of that "block"


Code:
# nawk '/ProcessType:/ && !p {i=-4;p=1} i++ > 0' file
ProcessType:    PROCESS_A (0)
SequenceNumber: 8286

2009 Mar 07  22:04:23:679   0:8286

ProcessType:    PROCESS_B (68)
SequenceNumber: 40689

2009 Mar 07  22:04:23:698  68:40689

DATA that should not be deleted

ProcessType:    PROCESS_B (68)
SequenceNumber: 40690
...

  #4 (permalink)  
Old 03-19-2009
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,345
I misunderstood the question, this should delete the first 4 lines in all blocks:


Code:
nawk '/ProcessType:/{i=-4} i++ > 0' file

Regards
  #5 (permalink)  
Old 03-19-2009
Juha Juha is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 36
Great! It works perfectly! thanks
  #6 (permalink)  
Old 03-19-2009
Shahul's Avatar
Shahul Shahul is offline
Registered User
  
 

Join Date: Aug 2008
Location: India
Posts: 175
Hi,

Hope this also can help you..


Code:
sed -n  '/ProcessType:/ {n;n;n;n;n;p}' out1.lst

Thanks
Sha
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 08:12 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