SED: delete matching row and 4 next rows?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED: delete matching row and 4 next rows?
# 1  
Old 03-19-2009
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  
Old 03-19-2009
If awk is allowed:

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

Regards
# 3  
Old 03-19-2009
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  
Old 03-19-2009
I misunderstood the question, this should delete the first 4 lines in all blocks:

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

Regards
# 5  
Old 03-19-2009
Great! It works perfectly! thanks Smilie
# 6  
Old 03-19-2009
Hi,

Hope this also can help you..

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

Thanks
Sha
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

2. Shell Programming and Scripting

Seemingly simple sed, delete between matching lines

There are many matching blocks of text in one file that need to be deleted. This example below is one block that needs to be either deleted or replaced with an empty line. This text below is the input file. The ouput file should be empty Searching Checks. Based on search criteria name: Value :... (2 Replies)
Discussion started by: bash_in_my_head
2 Replies

3. Shell Programming and Scripting

Print various rows in one row

I have this in a file 11.22.33.44 yyyyyyuser With awk/sed, I need this to be output as follows alias server.domain.com='ssh yyyyyyuser@11.22.33.44' (4 Replies)
Discussion started by: anil510
4 Replies

4. Shell Programming and Scripting

Awk/sed script for transposing any number of rows with header row

Greetings! I have been trying to find out a way to take a CSV file with a large number of rows, and a very large number of columns (in the thousands) and convert the rows to a single column of data, where the first row is a header representing the attribute name and the subsequent series of... (3 Replies)
Discussion started by: tntelle
3 Replies

5. Shell Programming and Scripting

sed or awk delete character in the lines before and after the matching line

Sample file: This is line one, this is another line, this is the PRIMARY INDEX line l ; This is another line The command should find the line with “PRIMARY INDEX” and remove the last character from the line preceding it (in this case , comma) and remove the first character from the line... (5 Replies)
Discussion started by: KC_Rules
5 Replies

6. Shell Programming and Scripting

Using sed (or awk or perl) to delete rows in a file

I have a Unix file with 200,000 records, and need to remove all records from the file that have the character ‘I' in position 68 (68 bytes from the left). I have searched for similar problems and it appears that it would be possible with sed, awk or perl but I do not know enough about any of these... (7 Replies)
Discussion started by: joddo
7 Replies

7. Shell Programming and Scripting

Combining multiple rows in single row based on certain condition using awk or sed

Hi, I'm using AIX(ksh shell). > cat temp.txt "a","b",0 "c",bc",0 "a1","b1",0 "cc","cb",1 "cc","b2",1 "bb","bc",2 I want the output as: "a","b","c","bc","a1","b1" "cc","cb","cc","b2" "bb","bc" I want to combine multiple lines into single line where third column is same. Is... (1 Reply)
Discussion started by: samuelray
1 Replies

8. Shell Programming and Scripting

sed/awk: Delete matching words leaving only the first instance

I have an input text that looks like this (comes already sorted): on Caturday 22 at 10:15, some event on Caturday 22 at 10:15, some other event on Caturday 22 at 21:30, even more events on Funday 23 at 11:00, yet another event I need to delete all the matching words between the lines, from... (2 Replies)
Discussion started by: GrinningArmor
2 Replies

9. Shell Programming and Scripting

sed find matching pattern delete next line

trying to use sed in finding a matching pattern in a file then deleting the next line only .. pattern --> <ad-content> I tried this but it results are not what I wish sed '/<ad-content>/{N;d;}' akv.xml > akv5.xml ex, <Celebrant2First>Mickey</Celebrant2First> <ad-content> Minnie... (2 Replies)
Discussion started by: aveitas
2 Replies

10. Shell Programming and Scripting

sed command to delete row

I want to use sed command to delete a matched row with a pattern. And I also want to delete previous and next row of that row. Which option can I use with sed ? (9 Replies)
Discussion started by: anhtt
9 Replies
Login or Register to Ask a Question