regex to add missing quotes










3















I am trying to add missing quotes at the ends of some lines in a text file.



I find that the regex [^"]$ suffices to find lines with missing terminal doublequotes and so tried the following replacement using a backreference (which tbh I've never used before). Using parens around the 'capture group' I hoped that sed would allow backreference to that group, but



sed 's|([^"]$)|1"|g' bigfile.tsv


hits



sed: -e expression #1, char 17: invalid reference 1 on `s' command's RHS


and same if I don't escape the replacement quotes



sed 's|([^"]$)|1"|g' bigfile.tsv


(tho now its char 16 that's offensive) . How does the backreference go? https://xkcd.com/1171/










share|improve this question




























    3















    I am trying to add missing quotes at the ends of some lines in a text file.



    I find that the regex [^"]$ suffices to find lines with missing terminal doublequotes and so tried the following replacement using a backreference (which tbh I've never used before). Using parens around the 'capture group' I hoped that sed would allow backreference to that group, but



    sed 's|([^"]$)|1"|g' bigfile.tsv


    hits



    sed: -e expression #1, char 17: invalid reference 1 on `s' command's RHS


    and same if I don't escape the replacement quotes



    sed 's|([^"]$)|1"|g' bigfile.tsv


    (tho now its char 16 that's offensive) . How does the backreference go? https://xkcd.com/1171/










    share|improve this question


























      3












      3








      3








      I am trying to add missing quotes at the ends of some lines in a text file.



      I find that the regex [^"]$ suffices to find lines with missing terminal doublequotes and so tried the following replacement using a backreference (which tbh I've never used before). Using parens around the 'capture group' I hoped that sed would allow backreference to that group, but



      sed 's|([^"]$)|1"|g' bigfile.tsv


      hits



      sed: -e expression #1, char 17: invalid reference 1 on `s' command's RHS


      and same if I don't escape the replacement quotes



      sed 's|([^"]$)|1"|g' bigfile.tsv


      (tho now its char 16 that's offensive) . How does the backreference go? https://xkcd.com/1171/










      share|improve this question
















      I am trying to add missing quotes at the ends of some lines in a text file.



      I find that the regex [^"]$ suffices to find lines with missing terminal doublequotes and so tried the following replacement using a backreference (which tbh I've never used before). Using parens around the 'capture group' I hoped that sed would allow backreference to that group, but



      sed 's|([^"]$)|1"|g' bigfile.tsv


      hits



      sed: -e expression #1, char 17: invalid reference 1 on `s' command's RHS


      and same if I don't escape the replacement quotes



      sed 's|([^"]$)|1"|g' bigfile.tsv


      (tho now its char 16 that's offensive) . How does the backreference go? https://xkcd.com/1171/







      sed regular-expression






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 14:47







      jeremy_rutman

















      asked Nov 12 '18 at 14:41









      jeremy_rutmanjeremy_rutman

      152111




      152111




















          2 Answers
          2






          active

          oldest

          votes


















          6














          When you run sed without -E, then the expression is a basic regular expression and the capture groups must be written as (...). When you use -E to enable extended regular expressions, capture groups are written (...).



          The inside [...] is literal, so your expression would also avoid adding a double quote on lines ending with . Some of the other escaping is also unnecessary.



          Therefore, you may write your sed command as



          sed 's/([^"])$/1"/'


          or as



          sed -E 's/([^"])$/1"/'


          Or, using &:



          sed 's/[^"]$/&"/'


          The & in the replacement part of the expression will be substituted by the part of the input that matched the regular expression.



          A couple of other alternatives that does not use a capture group:



          sed '/[^"]$/ s/$/"/'


          This applies s/$/"/ to all lines that matches /[^"]$/.



          Or, alternatively,



          sed '/"$/ !s/$/"/'


          This applies s/$/"/ to all lines that don't match /"$/ (there's a slight difference from the other approaches here in that it also adds a " to empty lines).



          Note that in all cases, the g flag at the end is definitely not needed.






          share|improve this answer

























          • Thanks for the detailed answer, this cleared up some mysteries for me

            – jeremy_rutman
            Nov 13 '18 at 13:13


















          2














          Try sed -e 's|([^"]$)|1"|g' bigfile.tsv.






          share|improve this answer


















          • 1





            thank you sir, that did the trick.

            – jeremy_rutman
            Nov 12 '18 at 14:55











          • This still does not add the missing " to lines ending with backslash and it needlessly uses g at the end (an anchored expression can only match once).

            – Kusalananda
            Nov 12 '18 at 15:04












          • I just provided a hint about the error message; I did not intend to solve the whole problem for the author. Mostly because I think the author will learn more when doing it himself or herself.

            – U. Windl
            Nov 12 '18 at 15:09











          • @U.Windl They may, or they may not ever notice.

            – Kusalananda
            Nov 12 '18 at 15:39










          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2funix.stackexchange.com%2fquestions%2f481273%2fregex-to-add-missing-quotes%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          6














          When you run sed without -E, then the expression is a basic regular expression and the capture groups must be written as (...). When you use -E to enable extended regular expressions, capture groups are written (...).



          The inside [...] is literal, so your expression would also avoid adding a double quote on lines ending with . Some of the other escaping is also unnecessary.



          Therefore, you may write your sed command as



          sed 's/([^"])$/1"/'


          or as



          sed -E 's/([^"])$/1"/'


          Or, using &:



          sed 's/[^"]$/&"/'


          The & in the replacement part of the expression will be substituted by the part of the input that matched the regular expression.



          A couple of other alternatives that does not use a capture group:



          sed '/[^"]$/ s/$/"/'


          This applies s/$/"/ to all lines that matches /[^"]$/.



          Or, alternatively,



          sed '/"$/ !s/$/"/'


          This applies s/$/"/ to all lines that don't match /"$/ (there's a slight difference from the other approaches here in that it also adds a " to empty lines).



          Note that in all cases, the g flag at the end is definitely not needed.






          share|improve this answer

























          • Thanks for the detailed answer, this cleared up some mysteries for me

            – jeremy_rutman
            Nov 13 '18 at 13:13















          6














          When you run sed without -E, then the expression is a basic regular expression and the capture groups must be written as (...). When you use -E to enable extended regular expressions, capture groups are written (...).



          The inside [...] is literal, so your expression would also avoid adding a double quote on lines ending with . Some of the other escaping is also unnecessary.



          Therefore, you may write your sed command as



          sed 's/([^"])$/1"/'


          or as



          sed -E 's/([^"])$/1"/'


          Or, using &:



          sed 's/[^"]$/&"/'


          The & in the replacement part of the expression will be substituted by the part of the input that matched the regular expression.



          A couple of other alternatives that does not use a capture group:



          sed '/[^"]$/ s/$/"/'


          This applies s/$/"/ to all lines that matches /[^"]$/.



          Or, alternatively,



          sed '/"$/ !s/$/"/'


          This applies s/$/"/ to all lines that don't match /"$/ (there's a slight difference from the other approaches here in that it also adds a " to empty lines).



          Note that in all cases, the g flag at the end is definitely not needed.






          share|improve this answer

























          • Thanks for the detailed answer, this cleared up some mysteries for me

            – jeremy_rutman
            Nov 13 '18 at 13:13













          6












          6








          6







          When you run sed without -E, then the expression is a basic regular expression and the capture groups must be written as (...). When you use -E to enable extended regular expressions, capture groups are written (...).



          The inside [...] is literal, so your expression would also avoid adding a double quote on lines ending with . Some of the other escaping is also unnecessary.



          Therefore, you may write your sed command as



          sed 's/([^"])$/1"/'


          or as



          sed -E 's/([^"])$/1"/'


          Or, using &:



          sed 's/[^"]$/&"/'


          The & in the replacement part of the expression will be substituted by the part of the input that matched the regular expression.



          A couple of other alternatives that does not use a capture group:



          sed '/[^"]$/ s/$/"/'


          This applies s/$/"/ to all lines that matches /[^"]$/.



          Or, alternatively,



          sed '/"$/ !s/$/"/'


          This applies s/$/"/ to all lines that don't match /"$/ (there's a slight difference from the other approaches here in that it also adds a " to empty lines).



          Note that in all cases, the g flag at the end is definitely not needed.






          share|improve this answer















          When you run sed without -E, then the expression is a basic regular expression and the capture groups must be written as (...). When you use -E to enable extended regular expressions, capture groups are written (...).



          The inside [...] is literal, so your expression would also avoid adding a double quote on lines ending with . Some of the other escaping is also unnecessary.



          Therefore, you may write your sed command as



          sed 's/([^"])$/1"/'


          or as



          sed -E 's/([^"])$/1"/'


          Or, using &:



          sed 's/[^"]$/&"/'


          The & in the replacement part of the expression will be substituted by the part of the input that matched the regular expression.



          A couple of other alternatives that does not use a capture group:



          sed '/[^"]$/ s/$/"/'


          This applies s/$/"/ to all lines that matches /[^"]$/.



          Or, alternatively,



          sed '/"$/ !s/$/"/'


          This applies s/$/"/ to all lines that don't match /"$/ (there's a slight difference from the other approaches here in that it also adds a " to empty lines).



          Note that in all cases, the g flag at the end is definitely not needed.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 '18 at 17:44









          Stéphane Chazelas

          301k55565917




          301k55565917










          answered Nov 12 '18 at 14:56









          KusalanandaKusalananda

          125k16236389




          125k16236389












          • Thanks for the detailed answer, this cleared up some mysteries for me

            – jeremy_rutman
            Nov 13 '18 at 13:13

















          • Thanks for the detailed answer, this cleared up some mysteries for me

            – jeremy_rutman
            Nov 13 '18 at 13:13
















          Thanks for the detailed answer, this cleared up some mysteries for me

          – jeremy_rutman
          Nov 13 '18 at 13:13





          Thanks for the detailed answer, this cleared up some mysteries for me

          – jeremy_rutman
          Nov 13 '18 at 13:13













          2














          Try sed -e 's|([^"]$)|1"|g' bigfile.tsv.






          share|improve this answer


















          • 1





            thank you sir, that did the trick.

            – jeremy_rutman
            Nov 12 '18 at 14:55











          • This still does not add the missing " to lines ending with backslash and it needlessly uses g at the end (an anchored expression can only match once).

            – Kusalananda
            Nov 12 '18 at 15:04












          • I just provided a hint about the error message; I did not intend to solve the whole problem for the author. Mostly because I think the author will learn more when doing it himself or herself.

            – U. Windl
            Nov 12 '18 at 15:09











          • @U.Windl They may, or they may not ever notice.

            – Kusalananda
            Nov 12 '18 at 15:39















          2














          Try sed -e 's|([^"]$)|1"|g' bigfile.tsv.






          share|improve this answer


















          • 1





            thank you sir, that did the trick.

            – jeremy_rutman
            Nov 12 '18 at 14:55











          • This still does not add the missing " to lines ending with backslash and it needlessly uses g at the end (an anchored expression can only match once).

            – Kusalananda
            Nov 12 '18 at 15:04












          • I just provided a hint about the error message; I did not intend to solve the whole problem for the author. Mostly because I think the author will learn more when doing it himself or herself.

            – U. Windl
            Nov 12 '18 at 15:09











          • @U.Windl They may, or they may not ever notice.

            – Kusalananda
            Nov 12 '18 at 15:39













          2












          2








          2







          Try sed -e 's|([^"]$)|1"|g' bigfile.tsv.






          share|improve this answer













          Try sed -e 's|([^"]$)|1"|g' bigfile.tsv.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 '18 at 14:51









          U. WindlU. Windl

          1315




          1315







          • 1





            thank you sir, that did the trick.

            – jeremy_rutman
            Nov 12 '18 at 14:55











          • This still does not add the missing " to lines ending with backslash and it needlessly uses g at the end (an anchored expression can only match once).

            – Kusalananda
            Nov 12 '18 at 15:04












          • I just provided a hint about the error message; I did not intend to solve the whole problem for the author. Mostly because I think the author will learn more when doing it himself or herself.

            – U. Windl
            Nov 12 '18 at 15:09











          • @U.Windl They may, or they may not ever notice.

            – Kusalananda
            Nov 12 '18 at 15:39












          • 1





            thank you sir, that did the trick.

            – jeremy_rutman
            Nov 12 '18 at 14:55











          • This still does not add the missing " to lines ending with backslash and it needlessly uses g at the end (an anchored expression can only match once).

            – Kusalananda
            Nov 12 '18 at 15:04












          • I just provided a hint about the error message; I did not intend to solve the whole problem for the author. Mostly because I think the author will learn more when doing it himself or herself.

            – U. Windl
            Nov 12 '18 at 15:09











          • @U.Windl They may, or they may not ever notice.

            – Kusalananda
            Nov 12 '18 at 15:39







          1




          1





          thank you sir, that did the trick.

          – jeremy_rutman
          Nov 12 '18 at 14:55





          thank you sir, that did the trick.

          – jeremy_rutman
          Nov 12 '18 at 14:55













          This still does not add the missing " to lines ending with backslash and it needlessly uses g at the end (an anchored expression can only match once).

          – Kusalananda
          Nov 12 '18 at 15:04






          This still does not add the missing " to lines ending with backslash and it needlessly uses g at the end (an anchored expression can only match once).

          – Kusalananda
          Nov 12 '18 at 15:04














          I just provided a hint about the error message; I did not intend to solve the whole problem for the author. Mostly because I think the author will learn more when doing it himself or herself.

          – U. Windl
          Nov 12 '18 at 15:09





          I just provided a hint about the error message; I did not intend to solve the whole problem for the author. Mostly because I think the author will learn more when doing it himself or herself.

          – U. Windl
          Nov 12 '18 at 15:09













          @U.Windl They may, or they may not ever notice.

          – Kusalananda
          Nov 12 '18 at 15:39





          @U.Windl They may, or they may not ever notice.

          – Kusalananda
          Nov 12 '18 at 15:39

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • 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%2funix.stackexchange.com%2fquestions%2f481273%2fregex-to-add-missing-quotes%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







          Popular posts from this blog

          Kleinkühnau

          Makov (Slowakei)

          Deutsches Schauspielhaus