Deleting parentheses in shell script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite
1












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.







share|improve this question






















  • Can brackets be nested?
    – Weijun Zhou
    Jan 30 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 at 20:14














up vote
3
down vote

favorite
1












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.







share|improve this question






















  • Can brackets be nested?
    – Weijun Zhou
    Jan 30 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 at 20:14












up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





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.







share|improve this question














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.









share|improve this question













share|improve this question




share|improve this question








edited Feb 2 at 11:47









Vlastimil

6,4011146119




6,4011146119










asked Jan 30 at 19:53









SeSa

284




284











  • Can brackets be nested?
    – Weijun Zhou
    Jan 30 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 at 20:14
















  • Can brackets be nested?
    – Weijun Zhou
    Jan 30 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 at 20:14















Can brackets be nested?
– Weijun Zhou
Jan 30 at 20:00




Can brackets be nested?
– Weijun Zhou
Jan 30 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 at 20:14




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 at 20:14










2 Answers
2






active

oldest

votes

















up vote
4
down vote



accepted










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





share|improve this answer





























    up vote
    5
    down vote













    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'





    share|improve this answer






















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f420778%2fdeleting-parentheses-in-shell-script%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      4
      down vote



      accepted










      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





      share|improve this answer


























        up vote
        4
        down vote



        accepted










        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





        share|improve this answer
























          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          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





          share|improve this answer














          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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 2 at 10:15









          Henk Langeveld

          547213




          547213










          answered Jan 31 at 1:23









          G-Man

          11.5k82657




          11.5k82657






















              up vote
              5
              down vote













              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'





              share|improve this answer


























                up vote
                5
                down vote













                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'





                share|improve this answer
























                  up vote
                  5
                  down vote










                  up vote
                  5
                  down vote









                  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'





                  share|improve this answer














                  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'






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 2 at 13:16









                  Stéphane Chazelas

                  281k53516847




                  281k53516847










                  answered Jan 31 at 20:59









                  ctac_

                  1,016116




                  1,016116






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f420778%2fdeleting-parentheses-in-shell-script%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay