0
#====================================================================== #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.

3
  • You were nearly there. When you quoted ( placed a \ before a character) the ( and ) you changed their meaning into a group instead of a literal ( or ). Perhaps a function f () { sed -i '/^gen (/{:a;N;/^)/M!ba;/'"$1"'/d' "${2:-file}"; } would meet your requirements (if you use bash)? Commented Feb 7, 2020 at 14:13
  • It is bash. I was getting an error and the parentheses were being indicated as the reason, so I thought I might have to escape them. Commented Feb 7, 2020 at 14:52
  • It is safer to surround sed commands by single quotes (as in my first comment) and then the side effects of bash interpolation will not occur. Commented Feb 8, 2020 at 17:46

1 Answer 1

1

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

awk -v RS=')' -v ORS=')' '!/id +112/' file 
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you so much, I did notice that I could do something with the closing parentheses, but I'm really inexperienced with sed, and even more inexperienced with awk. But I'm definitely noticing that it's more than worthwhile to really learn it. Thanks again.
Tested on a Mac, which platform are you on?
I'm using red hat linux
Actually just noticed it leaves a parentehese
*at the end of the file with the edited solution, no matter where I delete, middle beginning or end, extra parenthesis at the end.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.