sed on osx - extract all text that is between square brackets

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











up vote
2
down vote

favorite












Given this stream:



[foo] 123 [bar]
[gar] dsa [har] 345
[uf] 88 [gc] 43 [br]


I want to process this with sed (or anything else) so the output would be



foo bar
gar har
uf gc br


Tried cat myfile | sed -e 's/^.*[//;s/].*$//'



But it gives me only the last instance



My real input is something like:



53f42d4 [the contacts are duplicated] Adding support in picking email verified users [https://trello.com/c/663]
3c454b0 [the contacts are duplicated] splitting contact by phone numbers and emails and changing contact model to contain only 1 email [https://trello.com/c/663]
0e63e5b [we should not let a user confirm his email if we have a user with this confirmed email already] better doc [https://trello.com/c/643]
02671b7 [we should not let a user confirm his email if we have a user with this confirmed email already] preventing updating email if already in used by other user [https://trello.com/c/643]


So I'd like to get for the first line:



the contacts are duplicated https://trello.com/c/663


Thanks










share|improve this question









New contributor




YardenST is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    2
    down vote

    favorite












    Given this stream:



    [foo] 123 [bar]
    [gar] dsa [har] 345
    [uf] 88 [gc] 43 [br]


    I want to process this with sed (or anything else) so the output would be



    foo bar
    gar har
    uf gc br


    Tried cat myfile | sed -e 's/^.*[//;s/].*$//'



    But it gives me only the last instance



    My real input is something like:



    53f42d4 [the contacts are duplicated] Adding support in picking email verified users [https://trello.com/c/663]
    3c454b0 [the contacts are duplicated] splitting contact by phone numbers and emails and changing contact model to contain only 1 email [https://trello.com/c/663]
    0e63e5b [we should not let a user confirm his email if we have a user with this confirmed email already] better doc [https://trello.com/c/643]
    02671b7 [we should not let a user confirm his email if we have a user with this confirmed email already] preventing updating email if already in used by other user [https://trello.com/c/643]


    So I'd like to get for the first line:



    the contacts are duplicated https://trello.com/c/663


    Thanks










    share|improve this question









    New contributor




    YardenST is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Given this stream:



      [foo] 123 [bar]
      [gar] dsa [har] 345
      [uf] 88 [gc] 43 [br]


      I want to process this with sed (or anything else) so the output would be



      foo bar
      gar har
      uf gc br


      Tried cat myfile | sed -e 's/^.*[//;s/].*$//'



      But it gives me only the last instance



      My real input is something like:



      53f42d4 [the contacts are duplicated] Adding support in picking email verified users [https://trello.com/c/663]
      3c454b0 [the contacts are duplicated] splitting contact by phone numbers and emails and changing contact model to contain only 1 email [https://trello.com/c/663]
      0e63e5b [we should not let a user confirm his email if we have a user with this confirmed email already] better doc [https://trello.com/c/643]
      02671b7 [we should not let a user confirm his email if we have a user with this confirmed email already] preventing updating email if already in used by other user [https://trello.com/c/643]


      So I'd like to get for the first line:



      the contacts are duplicated https://trello.com/c/663


      Thanks










      share|improve this question









      New contributor




      YardenST is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      Given this stream:



      [foo] 123 [bar]
      [gar] dsa [har] 345
      [uf] 88 [gc] 43 [br]


      I want to process this with sed (or anything else) so the output would be



      foo bar
      gar har
      uf gc br


      Tried cat myfile | sed -e 's/^.*[//;s/].*$//'



      But it gives me only the last instance



      My real input is something like:



      53f42d4 [the contacts are duplicated] Adding support in picking email verified users [https://trello.com/c/663]
      3c454b0 [the contacts are duplicated] splitting contact by phone numbers and emails and changing contact model to contain only 1 email [https://trello.com/c/663]
      0e63e5b [we should not let a user confirm his email if we have a user with this confirmed email already] better doc [https://trello.com/c/643]
      02671b7 [we should not let a user confirm his email if we have a user with this confirmed email already] preventing updating email if already in used by other user [https://trello.com/c/643]


      So I'd like to get for the first line:



      the contacts are duplicated https://trello.com/c/663


      Thanks







      sed osx regular-expression






      share|improve this question









      New contributor




      YardenST is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      YardenST is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 2 hours ago





















      New contributor




      YardenST is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 hours ago









      YardenST

      1135




      1135




      New contributor




      YardenST is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      YardenST is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      YardenST is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          5 Answers
          5






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          awk works well for this too: using [ or ] as the field separator, print every even-numbered field:



          awk -F '' 'for (i=2; i<=NF; i+=2) printf "%s ", $i; print ""' file


          With sed, I'd write



          sed -E 's/(^|])[^*($|[)/ /g' file





          share|improve this answer




















          • The sed silution adds leading and trailing spaces (per line).
            – Isaac
            1 hour ago

















          up vote
          2
          down vote













          An idiomatic way to do that is using look around assertions, see e.g. https://www.regular-expressions.info/lookaround.html, but these are not supported in sed, only in PCRE compliant RE processors.



          Since Perl should be available on macos by default, perhaps this is viable alternative.



          Using Perl, you could say



          perl -pe 's/.+?(?<=[)(.+?)(?=]).+?/$1 /g'


          (note that this adds a space at the end of the line)



          For an explanation of the pattern, see https://regexr.com/41gi5






          share|improve this answer








          New contributor




          wwerner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.

















          • Nice. An alternative: perl -nE 'say join " ", (/ (?<=[) .*? (?=]) /xg)' file
            – glenn jackman
            1 hour ago


















          up vote
          1
          down vote













          This seems to work:



          $ sed -E 's/ [^[a-zA-Z0-9][^]]/ /g;s/ +/ /g' input | tr -d ''
          foo bar
          gar har
          uf gc br





          share|improve this answer




















          • thanks, but I get sed: input: No such file or directory
            – YardenST
            2 hours ago











          • @YardenST "input" was a placeholder for an input file you would need to supply yours there or omit that and pipe in the source.
            – Chris Stratton
            2 hours ago










          • Thanks got it :) but it does not seem to work with my input. My input is coming from git log commit1..commit2 --oneline. Ive updated the question with an example
            – YardenST
            2 hours ago


















          up vote
          1
          down vote













          sed -n '/[/ s-[^*--; s-[([^]]*)][^*- 1-g; s- --p '

          The algorithm is:

          ignore lines that do not contain brackets,

          remove text before first bracket,

          replace pairs of backets and optional trailing text by spaces, leaving the text inside the brackets,

          and remove the initial space, leaving only spaces in between.






          share|improve this answer



























            up vote
            1
            down vote













            This will match anything inside the first (opening) square bracket to the first (closing) square backet that follows, rep̀eatly.



            $ sed 's/[^*[([^]]*)][^*/1 /g' file
            foo bar
            gar har
            uf gc br


            Description:



            sed ' # start a sed script
            s/ # start a substitute command
            [^* # match all leading characters (except [)
            [ # match an explicit [
            ([^]]*) # capture text inside brackets.
            ] # match the closing ]
            [^* # match trailing text (if any).
            / 1/ # replace everything matched by the captured text.
            g # repeat for all the line.
            ' file # close script. Apply to file.


            This add a trailing space per match. If that must be removed, add a removal at the end:



            sed -e 's/[^*[([^]]*)][^*/1 /g' -e 's/ $//' file


            If you have GNU grep, this may help (one line per capture).



            grep -Po '[K[^]]*(?=])'


            And, if the above doesn't work, awk could also do it:



            awk 'print gensub(/[([^]]*)][^*/,"\1 ","g")' file





            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
              );



              );






              YardenST is a new contributor. Be nice, and check out our Code of Conduct.









               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f476352%2fsed-on-osx-extract-all-text-that-is-between-square-brackets%23new-answer', 'question_page');

              );

              Post as a guest






























              5 Answers
              5






              active

              oldest

              votes








              5 Answers
              5






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              3
              down vote



              accepted










              awk works well for this too: using [ or ] as the field separator, print every even-numbered field:



              awk -F '' 'for (i=2; i<=NF; i+=2) printf "%s ", $i; print ""' file


              With sed, I'd write



              sed -E 's/(^|])[^*($|[)/ /g' file





              share|improve this answer




















              • The sed silution adds leading and trailing spaces (per line).
                – Isaac
                1 hour ago














              up vote
              3
              down vote



              accepted










              awk works well for this too: using [ or ] as the field separator, print every even-numbered field:



              awk -F '' 'for (i=2; i<=NF; i+=2) printf "%s ", $i; print ""' file


              With sed, I'd write



              sed -E 's/(^|])[^*($|[)/ /g' file





              share|improve this answer




















              • The sed silution adds leading and trailing spaces (per line).
                – Isaac
                1 hour ago












              up vote
              3
              down vote



              accepted







              up vote
              3
              down vote



              accepted






              awk works well for this too: using [ or ] as the field separator, print every even-numbered field:



              awk -F '' 'for (i=2; i<=NF; i+=2) printf "%s ", $i; print ""' file


              With sed, I'd write



              sed -E 's/(^|])[^*($|[)/ /g' file





              share|improve this answer












              awk works well for this too: using [ or ] as the field separator, print every even-numbered field:



              awk -F '' 'for (i=2; i<=NF; i+=2) printf "%s ", $i; print ""' file


              With sed, I'd write



              sed -E 's/(^|])[^*($|[)/ /g' file






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 1 hour ago









              glenn jackman

              49.3k467106




              49.3k467106











              • The sed silution adds leading and trailing spaces (per line).
                – Isaac
                1 hour ago
















              • The sed silution adds leading and trailing spaces (per line).
                – Isaac
                1 hour ago















              The sed silution adds leading and trailing spaces (per line).
              – Isaac
              1 hour ago




              The sed silution adds leading and trailing spaces (per line).
              – Isaac
              1 hour ago












              up vote
              2
              down vote













              An idiomatic way to do that is using look around assertions, see e.g. https://www.regular-expressions.info/lookaround.html, but these are not supported in sed, only in PCRE compliant RE processors.



              Since Perl should be available on macos by default, perhaps this is viable alternative.



              Using Perl, you could say



              perl -pe 's/.+?(?<=[)(.+?)(?=]).+?/$1 /g'


              (note that this adds a space at the end of the line)



              For an explanation of the pattern, see https://regexr.com/41gi5






              share|improve this answer








              New contributor




              wwerner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.

















              • Nice. An alternative: perl -nE 'say join " ", (/ (?<=[) .*? (?=]) /xg)' file
                – glenn jackman
                1 hour ago















              up vote
              2
              down vote













              An idiomatic way to do that is using look around assertions, see e.g. https://www.regular-expressions.info/lookaround.html, but these are not supported in sed, only in PCRE compliant RE processors.



              Since Perl should be available on macos by default, perhaps this is viable alternative.



              Using Perl, you could say



              perl -pe 's/.+?(?<=[)(.+?)(?=]).+?/$1 /g'


              (note that this adds a space at the end of the line)



              For an explanation of the pattern, see https://regexr.com/41gi5






              share|improve this answer








              New contributor




              wwerner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.

















              • Nice. An alternative: perl -nE 'say join " ", (/ (?<=[) .*? (?=]) /xg)' file
                – glenn jackman
                1 hour ago













              up vote
              2
              down vote










              up vote
              2
              down vote









              An idiomatic way to do that is using look around assertions, see e.g. https://www.regular-expressions.info/lookaround.html, but these are not supported in sed, only in PCRE compliant RE processors.



              Since Perl should be available on macos by default, perhaps this is viable alternative.



              Using Perl, you could say



              perl -pe 's/.+?(?<=[)(.+?)(?=]).+?/$1 /g'


              (note that this adds a space at the end of the line)



              For an explanation of the pattern, see https://regexr.com/41gi5






              share|improve this answer








              New contributor




              wwerner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.









              An idiomatic way to do that is using look around assertions, see e.g. https://www.regular-expressions.info/lookaround.html, but these are not supported in sed, only in PCRE compliant RE processors.



              Since Perl should be available on macos by default, perhaps this is viable alternative.



              Using Perl, you could say



              perl -pe 's/.+?(?<=[)(.+?)(?=]).+?/$1 /g'


              (note that this adds a space at the end of the line)



              For an explanation of the pattern, see https://regexr.com/41gi5







              share|improve this answer








              New contributor




              wwerner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.









              share|improve this answer



              share|improve this answer






              New contributor




              wwerner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.









              answered 1 hour ago









              wwerner

              1212




              1212




              New contributor




              wwerner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.





              New contributor





              wwerner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






              wwerner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.











              • Nice. An alternative: perl -nE 'say join " ", (/ (?<=[) .*? (?=]) /xg)' file
                – glenn jackman
                1 hour ago

















              • Nice. An alternative: perl -nE 'say join " ", (/ (?<=[) .*? (?=]) /xg)' file
                – glenn jackman
                1 hour ago
















              Nice. An alternative: perl -nE 'say join " ", (/ (?<=[) .*? (?=]) /xg)' file
              – glenn jackman
              1 hour ago





              Nice. An alternative: perl -nE 'say join " ", (/ (?<=[) .*? (?=]) /xg)' file
              – glenn jackman
              1 hour ago











              up vote
              1
              down vote













              This seems to work:



              $ sed -E 's/ [^[a-zA-Z0-9][^]]/ /g;s/ +/ /g' input | tr -d ''
              foo bar
              gar har
              uf gc br





              share|improve this answer




















              • thanks, but I get sed: input: No such file or directory
                – YardenST
                2 hours ago











              • @YardenST "input" was a placeholder for an input file you would need to supply yours there or omit that and pipe in the source.
                – Chris Stratton
                2 hours ago










              • Thanks got it :) but it does not seem to work with my input. My input is coming from git log commit1..commit2 --oneline. Ive updated the question with an example
                – YardenST
                2 hours ago















              up vote
              1
              down vote













              This seems to work:



              $ sed -E 's/ [^[a-zA-Z0-9][^]]/ /g;s/ +/ /g' input | tr -d ''
              foo bar
              gar har
              uf gc br





              share|improve this answer




















              • thanks, but I get sed: input: No such file or directory
                – YardenST
                2 hours ago











              • @YardenST "input" was a placeholder for an input file you would need to supply yours there or omit that and pipe in the source.
                – Chris Stratton
                2 hours ago










              • Thanks got it :) but it does not seem to work with my input. My input is coming from git log commit1..commit2 --oneline. Ive updated the question with an example
                – YardenST
                2 hours ago













              up vote
              1
              down vote










              up vote
              1
              down vote









              This seems to work:



              $ sed -E 's/ [^[a-zA-Z0-9][^]]/ /g;s/ +/ /g' input | tr -d ''
              foo bar
              gar har
              uf gc br





              share|improve this answer












              This seems to work:



              $ sed -E 's/ [^[a-zA-Z0-9][^]]/ /g;s/ +/ /g' input | tr -d ''
              foo bar
              gar har
              uf gc br






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 2 hours ago









              DopeGhoti

              41.7k55180




              41.7k55180











              • thanks, but I get sed: input: No such file or directory
                – YardenST
                2 hours ago











              • @YardenST "input" was a placeholder for an input file you would need to supply yours there or omit that and pipe in the source.
                – Chris Stratton
                2 hours ago










              • Thanks got it :) but it does not seem to work with my input. My input is coming from git log commit1..commit2 --oneline. Ive updated the question with an example
                – YardenST
                2 hours ago

















              • thanks, but I get sed: input: No such file or directory
                – YardenST
                2 hours ago











              • @YardenST "input" was a placeholder for an input file you would need to supply yours there or omit that and pipe in the source.
                – Chris Stratton
                2 hours ago










              • Thanks got it :) but it does not seem to work with my input. My input is coming from git log commit1..commit2 --oneline. Ive updated the question with an example
                – YardenST
                2 hours ago
















              thanks, but I get sed: input: No such file or directory
              – YardenST
              2 hours ago





              thanks, but I get sed: input: No such file or directory
              – YardenST
              2 hours ago













              @YardenST "input" was a placeholder for an input file you would need to supply yours there or omit that and pipe in the source.
              – Chris Stratton
              2 hours ago




              @YardenST "input" was a placeholder for an input file you would need to supply yours there or omit that and pipe in the source.
              – Chris Stratton
              2 hours ago












              Thanks got it :) but it does not seem to work with my input. My input is coming from git log commit1..commit2 --oneline. Ive updated the question with an example
              – YardenST
              2 hours ago





              Thanks got it :) but it does not seem to work with my input. My input is coming from git log commit1..commit2 --oneline. Ive updated the question with an example
              – YardenST
              2 hours ago











              up vote
              1
              down vote













              sed -n '/[/ s-[^*--; s-[([^]]*)][^*- 1-g; s- --p '

              The algorithm is:

              ignore lines that do not contain brackets,

              remove text before first bracket,

              replace pairs of backets and optional trailing text by spaces, leaving the text inside the brackets,

              and remove the initial space, leaving only spaces in between.






              share|improve this answer
























                up vote
                1
                down vote













                sed -n '/[/ s-[^*--; s-[([^]]*)][^*- 1-g; s- --p '

                The algorithm is:

                ignore lines that do not contain brackets,

                remove text before first bracket,

                replace pairs of backets and optional trailing text by spaces, leaving the text inside the brackets,

                and remove the initial space, leaving only spaces in between.






                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  sed -n '/[/ s-[^*--; s-[([^]]*)][^*- 1-g; s- --p '

                  The algorithm is:

                  ignore lines that do not contain brackets,

                  remove text before first bracket,

                  replace pairs of backets and optional trailing text by spaces, leaving the text inside the brackets,

                  and remove the initial space, leaving only spaces in between.






                  share|improve this answer












                  sed -n '/[/ s-[^*--; s-[([^]]*)][^*- 1-g; s- --p '

                  The algorithm is:

                  ignore lines that do not contain brackets,

                  remove text before first bracket,

                  replace pairs of backets and optional trailing text by spaces, leaving the text inside the brackets,

                  and remove the initial space, leaving only spaces in between.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 hours ago









                  Juergen

                  787




                  787




















                      up vote
                      1
                      down vote













                      This will match anything inside the first (opening) square bracket to the first (closing) square backet that follows, rep̀eatly.



                      $ sed 's/[^*[([^]]*)][^*/1 /g' file
                      foo bar
                      gar har
                      uf gc br


                      Description:



                      sed ' # start a sed script
                      s/ # start a substitute command
                      [^* # match all leading characters (except [)
                      [ # match an explicit [
                      ([^]]*) # capture text inside brackets.
                      ] # match the closing ]
                      [^* # match trailing text (if any).
                      / 1/ # replace everything matched by the captured text.
                      g # repeat for all the line.
                      ' file # close script. Apply to file.


                      This add a trailing space per match. If that must be removed, add a removal at the end:



                      sed -e 's/[^*[([^]]*)][^*/1 /g' -e 's/ $//' file


                      If you have GNU grep, this may help (one line per capture).



                      grep -Po '[K[^]]*(?=])'


                      And, if the above doesn't work, awk could also do it:



                      awk 'print gensub(/[([^]]*)][^*/,"\1 ","g")' file





                      share|improve this answer


























                        up vote
                        1
                        down vote













                        This will match anything inside the first (opening) square bracket to the first (closing) square backet that follows, rep̀eatly.



                        $ sed 's/[^*[([^]]*)][^*/1 /g' file
                        foo bar
                        gar har
                        uf gc br


                        Description:



                        sed ' # start a sed script
                        s/ # start a substitute command
                        [^* # match all leading characters (except [)
                        [ # match an explicit [
                        ([^]]*) # capture text inside brackets.
                        ] # match the closing ]
                        [^* # match trailing text (if any).
                        / 1/ # replace everything matched by the captured text.
                        g # repeat for all the line.
                        ' file # close script. Apply to file.


                        This add a trailing space per match. If that must be removed, add a removal at the end:



                        sed -e 's/[^*[([^]]*)][^*/1 /g' -e 's/ $//' file


                        If you have GNU grep, this may help (one line per capture).



                        grep -Po '[K[^]]*(?=])'


                        And, if the above doesn't work, awk could also do it:



                        awk 'print gensub(/[([^]]*)][^*/,"\1 ","g")' file





                        share|improve this answer
























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          This will match anything inside the first (opening) square bracket to the first (closing) square backet that follows, rep̀eatly.



                          $ sed 's/[^*[([^]]*)][^*/1 /g' file
                          foo bar
                          gar har
                          uf gc br


                          Description:



                          sed ' # start a sed script
                          s/ # start a substitute command
                          [^* # match all leading characters (except [)
                          [ # match an explicit [
                          ([^]]*) # capture text inside brackets.
                          ] # match the closing ]
                          [^* # match trailing text (if any).
                          / 1/ # replace everything matched by the captured text.
                          g # repeat for all the line.
                          ' file # close script. Apply to file.


                          This add a trailing space per match. If that must be removed, add a removal at the end:



                          sed -e 's/[^*[([^]]*)][^*/1 /g' -e 's/ $//' file


                          If you have GNU grep, this may help (one line per capture).



                          grep -Po '[K[^]]*(?=])'


                          And, if the above doesn't work, awk could also do it:



                          awk 'print gensub(/[([^]]*)][^*/,"\1 ","g")' file





                          share|improve this answer














                          This will match anything inside the first (opening) square bracket to the first (closing) square backet that follows, rep̀eatly.



                          $ sed 's/[^*[([^]]*)][^*/1 /g' file
                          foo bar
                          gar har
                          uf gc br


                          Description:



                          sed ' # start a sed script
                          s/ # start a substitute command
                          [^* # match all leading characters (except [)
                          [ # match an explicit [
                          ([^]]*) # capture text inside brackets.
                          ] # match the closing ]
                          [^* # match trailing text (if any).
                          / 1/ # replace everything matched by the captured text.
                          g # repeat for all the line.
                          ' file # close script. Apply to file.


                          This add a trailing space per match. If that must be removed, add a removal at the end:



                          sed -e 's/[^*[([^]]*)][^*/1 /g' -e 's/ $//' file


                          If you have GNU grep, this may help (one line per capture).



                          grep -Po '[K[^]]*(?=])'


                          And, if the above doesn't work, awk could also do it:



                          awk 'print gensub(/[([^]]*)][^*/,"\1 ","g")' file






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 52 mins ago

























                          answered 1 hour ago









                          Isaac

                          8,50011140




                          8,50011140




















                              YardenST is a new contributor. Be nice, and check out our Code of Conduct.









                               

                              draft saved


                              draft discarded


















                              YardenST is a new contributor. Be nice, and check out our Code of Conduct.












                              YardenST is a new contributor. Be nice, and check out our Code of Conduct.











                              YardenST is a new contributor. Be nice, and check out our Code of Conduct.













                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f476352%2fsed-on-osx-extract-all-text-that-is-between-square-brackets%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Comments

                              Popular posts from this blog

                              What does second last employer means? [closed]

                              Installing NextGIS Connect into QGIS 3?

                              One-line joke