albuck97
albuck97

Reputation: 53

How to delete this pattern with sed or awk?

#======================================================================
#NAME
#======================================================================
gen (
         id  111
         descr  Test 1
         txcd  
                content           
)
#======================================================================
#NAME
#======================================================================
gen (
         id  112
         descr  Test 2
         txcd
                content
)
#======================================================================
#NAME
#======================================================================
gen (
         id  123
         descr  Test 3
         txcd
                content
)

I have this pattern, and have tried to find a way to delete a certain id using sed or awk. I pass the ID using a function parameter, $1. I've looked it up on stack but couldn't find a solution that worked for me.

sed -i "/gen \(/{:a;N;/\)/!ba};/$1/d" file

At a certain point when editing this sed command it did delete a line, but just the line where the ID itself was.

This is the one I've been working with, I've made small changes but nothing worked thus far. I'm supposed to delete the hashtags on top as well, but to begin with, I was mainly trying to delete the gen pattern.

Upvotes: 0

Views: 69

Answers (1)

tripleee
tripleee

Reputation: 189457

If a lone ) always terminates one record, this is easy enough in Awk.

awk -v RS=')' -v ORS=')' '!/id +112/' file

Upvotes: 1

Related Questions