Trying to do a simple sed substitution but I'm confused about what needs to be escaped










2















I have this string: '$'nwnwnwnnn



And want to change it to: bitset<9>(0bnwnwnwnnn), '$',



I've looked at many similar questions for different shells using their methods but nothing has worked. I'm generally in zsh but I can use bash or another shell.



The general form I've been trying is this:



sed -E -i new s/('.')([nw]+)/ bitset<9>(0b2), 1,/g thing.txt


It should work for any character other than $ and any sequence of n or w.



I'm generally confused as to what I need to escape here. Some answers on this site said to escape the parenthesis in the first part of the substitution.



Am I using -i incorrectly?










share|improve this question




























    2















    I have this string: '$'nwnwnwnnn



    And want to change it to: bitset<9>(0bnwnwnwnnn), '$',



    I've looked at many similar questions for different shells using their methods but nothing has worked. I'm generally in zsh but I can use bash or another shell.



    The general form I've been trying is this:



    sed -E -i new s/('.')([nw]+)/ bitset<9>(0b2), 1,/g thing.txt


    It should work for any character other than $ and any sequence of n or w.



    I'm generally confused as to what I need to escape here. Some answers on this site said to escape the parenthesis in the first part of the substitution.



    Am I using -i incorrectly?










    share|improve this question


























      2












      2








      2








      I have this string: '$'nwnwnwnnn



      And want to change it to: bitset<9>(0bnwnwnwnnn), '$',



      I've looked at many similar questions for different shells using their methods but nothing has worked. I'm generally in zsh but I can use bash or another shell.



      The general form I've been trying is this:



      sed -E -i new s/('.')([nw]+)/ bitset<9>(0b2), 1,/g thing.txt


      It should work for any character other than $ and any sequence of n or w.



      I'm generally confused as to what I need to escape here. Some answers on this site said to escape the parenthesis in the first part of the substitution.



      Am I using -i incorrectly?










      share|improve this question
















      I have this string: '$'nwnwnwnnn



      And want to change it to: bitset<9>(0bnwnwnwnnn), '$',



      I've looked at many similar questions for different shells using their methods but nothing has worked. I'm generally in zsh but I can use bash or another shell.



      The general form I've been trying is this:



      sed -E -i new s/('.')([nw]+)/ bitset<9>(0b2), 1,/g thing.txt


      It should work for any character other than $ and any sequence of n or w.



      I'm generally confused as to what I need to escape here. Some answers on this site said to escape the parenthesis in the first part of the substitution.



      Am I using -i incorrectly?







      regex shell sed






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 0:07









      Barmar

      429k36253352




      429k36253352










      asked Nov 15 '18 at 0:05









      alsozatchalsozatch

      111




      111






















          1 Answer
          1






          active

          oldest

          votes


















          1














          You need to escape the parentheses to create a capture group if you're using basic regexp, you don't escape them if you're using extended regexp. The -E option to GNU sed, and the -r option to standard sed, enable extended regexp, so you don't need to escape them.



          If you only want to match $ rather than allow any character in the quotes, you need an escaped $.



          You need to put the entire s/// command inside quotes, as it must be a single argument to the sed command.



          When using -i, it's conventional to put a . before the suffix. Also, the suffix is put on the saved copy of the original file, not the new file that you're creating with the changes, so new is a poor suffix.



          sed -E -i .bak "s/('$')([nw]+)/ bitset<9>(0b2), 1,/g" thing.txt





          share|improve this answer

























          • This worked for me but only when I used double quotes instead of single quotes around the regex command and stopped escaping the single quotes. Thanks

            – alsozatch
            Nov 15 '18 at 0:24











          • I always forget which shell quotes allow escaping quotes inside them :)

            – Barmar
            Nov 15 '18 at 0:26










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310616%2ftrying-to-do-a-simple-sed-substitution-but-im-confused-about-what-needs-to-be-e%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You need to escape the parentheses to create a capture group if you're using basic regexp, you don't escape them if you're using extended regexp. The -E option to GNU sed, and the -r option to standard sed, enable extended regexp, so you don't need to escape them.



          If you only want to match $ rather than allow any character in the quotes, you need an escaped $.



          You need to put the entire s/// command inside quotes, as it must be a single argument to the sed command.



          When using -i, it's conventional to put a . before the suffix. Also, the suffix is put on the saved copy of the original file, not the new file that you're creating with the changes, so new is a poor suffix.



          sed -E -i .bak "s/('$')([nw]+)/ bitset<9>(0b2), 1,/g" thing.txt





          share|improve this answer

























          • This worked for me but only when I used double quotes instead of single quotes around the regex command and stopped escaping the single quotes. Thanks

            – alsozatch
            Nov 15 '18 at 0:24











          • I always forget which shell quotes allow escaping quotes inside them :)

            – Barmar
            Nov 15 '18 at 0:26















          1














          You need to escape the parentheses to create a capture group if you're using basic regexp, you don't escape them if you're using extended regexp. The -E option to GNU sed, and the -r option to standard sed, enable extended regexp, so you don't need to escape them.



          If you only want to match $ rather than allow any character in the quotes, you need an escaped $.



          You need to put the entire s/// command inside quotes, as it must be a single argument to the sed command.



          When using -i, it's conventional to put a . before the suffix. Also, the suffix is put on the saved copy of the original file, not the new file that you're creating with the changes, so new is a poor suffix.



          sed -E -i .bak "s/('$')([nw]+)/ bitset<9>(0b2), 1,/g" thing.txt





          share|improve this answer

























          • This worked for me but only when I used double quotes instead of single quotes around the regex command and stopped escaping the single quotes. Thanks

            – alsozatch
            Nov 15 '18 at 0:24











          • I always forget which shell quotes allow escaping quotes inside them :)

            – Barmar
            Nov 15 '18 at 0:26













          1












          1








          1







          You need to escape the parentheses to create a capture group if you're using basic regexp, you don't escape them if you're using extended regexp. The -E option to GNU sed, and the -r option to standard sed, enable extended regexp, so you don't need to escape them.



          If you only want to match $ rather than allow any character in the quotes, you need an escaped $.



          You need to put the entire s/// command inside quotes, as it must be a single argument to the sed command.



          When using -i, it's conventional to put a . before the suffix. Also, the suffix is put on the saved copy of the original file, not the new file that you're creating with the changes, so new is a poor suffix.



          sed -E -i .bak "s/('$')([nw]+)/ bitset<9>(0b2), 1,/g" thing.txt





          share|improve this answer















          You need to escape the parentheses to create a capture group if you're using basic regexp, you don't escape them if you're using extended regexp. The -E option to GNU sed, and the -r option to standard sed, enable extended regexp, so you don't need to escape them.



          If you only want to match $ rather than allow any character in the quotes, you need an escaped $.



          You need to put the entire s/// command inside quotes, as it must be a single argument to the sed command.



          When using -i, it's conventional to put a . before the suffix. Also, the suffix is put on the saved copy of the original file, not the new file that you're creating with the changes, so new is a poor suffix.



          sed -E -i .bak "s/('$')([nw]+)/ bitset<9>(0b2), 1,/g" thing.txt






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 0:25

























          answered Nov 15 '18 at 0:13









          BarmarBarmar

          429k36253352




          429k36253352












          • This worked for me but only when I used double quotes instead of single quotes around the regex command and stopped escaping the single quotes. Thanks

            – alsozatch
            Nov 15 '18 at 0:24











          • I always forget which shell quotes allow escaping quotes inside them :)

            – Barmar
            Nov 15 '18 at 0:26

















          • This worked for me but only when I used double quotes instead of single quotes around the regex command and stopped escaping the single quotes. Thanks

            – alsozatch
            Nov 15 '18 at 0:24











          • I always forget which shell quotes allow escaping quotes inside them :)

            – Barmar
            Nov 15 '18 at 0:26
















          This worked for me but only when I used double quotes instead of single quotes around the regex command and stopped escaping the single quotes. Thanks

          – alsozatch
          Nov 15 '18 at 0:24





          This worked for me but only when I used double quotes instead of single quotes around the regex command and stopped escaping the single quotes. Thanks

          – alsozatch
          Nov 15 '18 at 0:24













          I always forget which shell quotes allow escaping quotes inside them :)

          – Barmar
          Nov 15 '18 at 0:26





          I always forget which shell quotes allow escaping quotes inside them :)

          – Barmar
          Nov 15 '18 at 0:26



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310616%2ftrying-to-do-a-simple-sed-substitution-but-im-confused-about-what-needs-to-be-e%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3

          Museum of Modern and Contemporary Art of Trento and Rovereto