-1

My apologizes... I have brain freeze and can't work through this. I'm using sed and pattern space, but I keep deleting all occurrences of the lines containing the pattern.

I want to delete only the first line containing the pattern, when followed by another line containing the same pattern.

The list I am using will never have more than two consecutive lines containing the same pattern

The pattern is "Query"

This my data:

Query: A ANNUAL SERVICE FEE                                                                          
       SourceField:               EXP-ANNUAL:       SourceTable:               MASTER
Query: A ANNUAL SERVICE FEE                                                                          
Query: A Edie ACTIVE LNS                                                                             
       SourceField:               LOAN-NO:      SourceTable:               MASTER
Query: A Edie ACTIVE LNS                                                                             
       SourceField:               LO:           SourceTable:               MASTER

This is what I want it to look like:

Query: A ANNUAL SERVICE FEE                                                                          
       SourceField:               EXP-ANNUAL:       SourceTable:               MASTER                                                                      
Query: A Edie ACTIVE LNS                                                                             
       SourceField:               LOAN-NO:      SourceTable:               MASTER
Query: A Edie ACTIVE LNS                                                                             
       SourceField:               LO:           SourceTable:               MASTER 

1 Answer 1

2
sed '/^Query/{N;/\nQuery/D;}' file

On lines which begin with Query, append next line to pattern space. If next line begins with Query, delete first line.

To avoid printing last line (if last line matches ^Query):

sed -n '/^Query/{N;/\nQuery/D;};p' file
1
  • Than you so much! This works very nicely
    – C0ppert0p
    Jul 10, 2020 at 4:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .