1

I have a multiple files that contains something like:

this is a test1
 {
test 123
test 456
test 789
}

this is a test2
 {
test 123
test 456
test 789
}

this is a test3
 {
test 123
test 456
test 789
}

Need to delete a section:

this is a test2
 {
test 123
test 456
test 789
}

lines between braces may be a different (less or more lines) I've tried something like :

sed -i 's|This is a test2 *.* !}||g' *

and

sed -i 's|This is a test2, !}||g' *

but no success

1

what about

sed -e '/this is a test2/,/}/d'

which basically

  • -e tell sed to use next pattern
  • /this is a test2/,/}/ select line between this is a test2 and }
  • d delete it

Usage

 sed -e '/this is a test2/,/}/d' A > B
  • apply sed from A file into B

    sed -i  -e '/this is a test2/,/}/d' A
    
  • edit directly into A

  • grr pesky indentation !! – Archemar Sep 7 '15 at 8:21
2

A simple approach with awk:

awk '/test2/,/^}/{next}1' file > newfile

(Has the disadvantage of leaving a blank line, which may not be acceptable...)

  • this is useful too, but the solution of the Archemar is better for multiple files. Thanks a lot – Tsvetan Krastanov Sep 7 '15 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.