I would like to create mechanism to delete parentheses, or parentheses and the text between them. For example:


text0 text1 text2 (text3 text4)


text0 text1 text2 text3 text4


text0 text1 text2

I would like to test both options and other type of brackets but I'm not sure what tool should I use, Awk or Sed or maybe something else? I would be grateful for any advice.

  • Can brackets be nested? – Weijun Zhou Jan 30 '18 at 20:00
  • Good question. Let's say yes but only brackets that are deeply nested - file may be json and curly braces will be preseted in every case. – SeSa Jan 30 '18 at 20:14

The other half of your question, deleting just the parentheses (but not the text between them):

echo 'text0 text1 text2 (text3 text4)' |
   sed 's/[()]//g'


text0 text1 text2 text3 text4

To keep the text, this is enough:

tr -d '()'

To delete the text and parentheses:

sed 's/([^)]*)//g;s/  / /g'

If the text contains nested parenthesis like :

echo 'text0 (text1 (textA )) text2 (text3 text4) test5' |
  sed -e :A -e 's/([^()]*)//;tA' -e 's/  / /g'

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.