6

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

before:

text0 text1 text2 (text3 text4)

after:

text0 text1 text2 text3 text4

or:

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.

2
  • Can brackets be nested? Jan 30, 2018 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, 2018 at 20:14

2 Answers 2

6

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'
0
4

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'

Output:

text0 text1 text2 text3 text4

You must log in to answer this question.

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