Sed to find and replace regular expression

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











up vote
1
down vote

favorite












I have a file that I need to replace all matches below:



," "n to ,"ZYY"n
,"ZZY"n to ,"ZYY"n
,"GMZ"n to ,"FPZ"n


I'm trying to use the command: sed 's/," "/,"ZYY"/g' Packs.txt but it is not working.










share|improve this question



















  • 2




    Please define not working. The syntax seems ok.
    – Rui F Ribeiro
    4 hours ago











  • When i run the command, don't replace as i wish.
    – Pedro Lopes
    3 hours ago














up vote
1
down vote

favorite












I have a file that I need to replace all matches below:



," "n to ,"ZYY"n
,"ZZY"n to ,"ZYY"n
,"GMZ"n to ,"FPZ"n


I'm trying to use the command: sed 's/," "/,"ZYY"/g' Packs.txt but it is not working.










share|improve this question



















  • 2




    Please define not working. The syntax seems ok.
    – Rui F Ribeiro
    4 hours ago











  • When i run the command, don't replace as i wish.
    – Pedro Lopes
    3 hours ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a file that I need to replace all matches below:



," "n to ,"ZYY"n
,"ZZY"n to ,"ZYY"n
,"GMZ"n to ,"FPZ"n


I'm trying to use the command: sed 's/," "/,"ZYY"/g' Packs.txt but it is not working.










share|improve this question















I have a file that I need to replace all matches below:



," "n to ,"ZYY"n
,"ZZY"n to ,"ZYY"n
,"GMZ"n to ,"FPZ"n


I'm trying to use the command: sed 's/," "/,"ZYY"/g' Packs.txt but it is not working.







sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 4 hours ago









Rui F Ribeiro

37.3k1374118




37.3k1374118










asked 4 hours ago









Pedro Lopes

62




62







  • 2




    Please define not working. The syntax seems ok.
    – Rui F Ribeiro
    4 hours ago











  • When i run the command, don't replace as i wish.
    – Pedro Lopes
    3 hours ago












  • 2




    Please define not working. The syntax seems ok.
    – Rui F Ribeiro
    4 hours ago











  • When i run the command, don't replace as i wish.
    – Pedro Lopes
    3 hours ago







2




2




Please define not working. The syntax seems ok.
– Rui F Ribeiro
4 hours ago





Please define not working. The syntax seems ok.
– Rui F Ribeiro
4 hours ago













When i run the command, don't replace as i wish.
– Pedro Lopes
3 hours ago




When i run the command, don't replace as i wish.
– Pedro Lopes
3 hours ago










3 Answers
3






active

oldest

votes

















up vote
2
down vote













If you have a collection of conversions you need to do, you can put them into a file. You can then use the "-f _sed_file_" to process all of them. For example, if you input file is something like this test.in file:



This," ",is,a,test
This,too,"ZZY",is,a,test
And,so,"GMZ",is,this


And you have the following sed file:



s/," "/,"ZYY"/g
s/,"ZZY"/,"ZYY"/g
s/,"GMZ"/,"FPZ"/g


You can run the following command:



sed -f test.sed test.in


To get the following output:



This,"ZYY",is,a,test
This,too,"ZYY",is,a,test
And,so,"FPZ",is,this


Hope this helps.






share|improve this answer




















  • Will fail with an unexpected replacement for cases not at the end of the line as described in the question.
    – DopeGhoti
    3 hours ago


















up vote
2
down vote













This appears to do the trick:



$ sed -E 's/,"( |ZZY)"$/,"ZYY"/;s/,"GMZ"$/,"FPZ"/' inputfile


The main thing here is using the $ anchor to mark the end of the line, rather than searching for a n.






share|improve this answer



























    up vote
    0
    down vote













    This will change all three strings to what you desire and only send the changes to standard output:



     sed -e 's|," "\n|,"ZYY"\n|g' -e 's|,"ZZY"\n|,"ZYY"\n|g' -e 's|,"GMZ"\n|,"FPZ"\n|g' Packs.txt


    I used the entire strings and not just the letters between the double quotes as you didn't specify whether those specific strings appear anywhere else in the file.



    Once you are satisfied with the outcome, you can use the below command to change the file itself.



    sed -i -e 's|," "\n|,"ZYY"\n|g' -e 's|,"ZZY"\n|,"ZYY"\n|g' -e 's|,"GMZ"\n|,"FPZ"\n|g' Packs.txt





    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%2f477085%2fsed-to-find-and-replace-regular-expression%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote













      If you have a collection of conversions you need to do, you can put them into a file. You can then use the "-f _sed_file_" to process all of them. For example, if you input file is something like this test.in file:



      This," ",is,a,test
      This,too,"ZZY",is,a,test
      And,so,"GMZ",is,this


      And you have the following sed file:



      s/," "/,"ZYY"/g
      s/,"ZZY"/,"ZYY"/g
      s/,"GMZ"/,"FPZ"/g


      You can run the following command:



      sed -f test.sed test.in


      To get the following output:



      This,"ZYY",is,a,test
      This,too,"ZYY",is,a,test
      And,so,"FPZ",is,this


      Hope this helps.






      share|improve this answer




















      • Will fail with an unexpected replacement for cases not at the end of the line as described in the question.
        – DopeGhoti
        3 hours ago















      up vote
      2
      down vote













      If you have a collection of conversions you need to do, you can put them into a file. You can then use the "-f _sed_file_" to process all of them. For example, if you input file is something like this test.in file:



      This," ",is,a,test
      This,too,"ZZY",is,a,test
      And,so,"GMZ",is,this


      And you have the following sed file:



      s/," "/,"ZYY"/g
      s/,"ZZY"/,"ZYY"/g
      s/,"GMZ"/,"FPZ"/g


      You can run the following command:



      sed -f test.sed test.in


      To get the following output:



      This,"ZYY",is,a,test
      This,too,"ZYY",is,a,test
      And,so,"FPZ",is,this


      Hope this helps.






      share|improve this answer




















      • Will fail with an unexpected replacement for cases not at the end of the line as described in the question.
        – DopeGhoti
        3 hours ago













      up vote
      2
      down vote










      up vote
      2
      down vote









      If you have a collection of conversions you need to do, you can put them into a file. You can then use the "-f _sed_file_" to process all of them. For example, if you input file is something like this test.in file:



      This," ",is,a,test
      This,too,"ZZY",is,a,test
      And,so,"GMZ",is,this


      And you have the following sed file:



      s/," "/,"ZYY"/g
      s/,"ZZY"/,"ZYY"/g
      s/,"GMZ"/,"FPZ"/g


      You can run the following command:



      sed -f test.sed test.in


      To get the following output:



      This,"ZYY",is,a,test
      This,too,"ZYY",is,a,test
      And,so,"FPZ",is,this


      Hope this helps.






      share|improve this answer












      If you have a collection of conversions you need to do, you can put them into a file. You can then use the "-f _sed_file_" to process all of them. For example, if you input file is something like this test.in file:



      This," ",is,a,test
      This,too,"ZZY",is,a,test
      And,so,"GMZ",is,this


      And you have the following sed file:



      s/," "/,"ZYY"/g
      s/,"ZZY"/,"ZYY"/g
      s/,"GMZ"/,"FPZ"/g


      You can run the following command:



      sed -f test.sed test.in


      To get the following output:



      This,"ZYY",is,a,test
      This,too,"ZYY",is,a,test
      And,so,"FPZ",is,this


      Hope this helps.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered 4 hours ago









      Lewis M

      5264




      5264











      • Will fail with an unexpected replacement for cases not at the end of the line as described in the question.
        – DopeGhoti
        3 hours ago

















      • Will fail with an unexpected replacement for cases not at the end of the line as described in the question.
        – DopeGhoti
        3 hours ago
















      Will fail with an unexpected replacement for cases not at the end of the line as described in the question.
      – DopeGhoti
      3 hours ago





      Will fail with an unexpected replacement for cases not at the end of the line as described in the question.
      – DopeGhoti
      3 hours ago













      up vote
      2
      down vote













      This appears to do the trick:



      $ sed -E 's/,"( |ZZY)"$/,"ZYY"/;s/,"GMZ"$/,"FPZ"/' inputfile


      The main thing here is using the $ anchor to mark the end of the line, rather than searching for a n.






      share|improve this answer
























        up vote
        2
        down vote













        This appears to do the trick:



        $ sed -E 's/,"( |ZZY)"$/,"ZYY"/;s/,"GMZ"$/,"FPZ"/' inputfile


        The main thing here is using the $ anchor to mark the end of the line, rather than searching for a n.






        share|improve this answer






















          up vote
          2
          down vote










          up vote
          2
          down vote









          This appears to do the trick:



          $ sed -E 's/,"( |ZZY)"$/,"ZYY"/;s/,"GMZ"$/,"FPZ"/' inputfile


          The main thing here is using the $ anchor to mark the end of the line, rather than searching for a n.






          share|improve this answer












          This appears to do the trick:



          $ sed -E 's/,"( |ZZY)"$/,"ZYY"/;s/,"GMZ"$/,"FPZ"/' inputfile


          The main thing here is using the $ anchor to mark the end of the line, rather than searching for a n.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 3 hours ago









          DopeGhoti

          41.8k55180




          41.8k55180




















              up vote
              0
              down vote













              This will change all three strings to what you desire and only send the changes to standard output:



               sed -e 's|," "\n|,"ZYY"\n|g' -e 's|,"ZZY"\n|,"ZYY"\n|g' -e 's|,"GMZ"\n|,"FPZ"\n|g' Packs.txt


              I used the entire strings and not just the letters between the double quotes as you didn't specify whether those specific strings appear anywhere else in the file.



              Once you are satisfied with the outcome, you can use the below command to change the file itself.



              sed -i -e 's|," "\n|,"ZYY"\n|g' -e 's|,"ZZY"\n|,"ZYY"\n|g' -e 's|,"GMZ"\n|,"FPZ"\n|g' Packs.txt





              share|improve this answer
























                up vote
                0
                down vote













                This will change all three strings to what you desire and only send the changes to standard output:



                 sed -e 's|," "\n|,"ZYY"\n|g' -e 's|,"ZZY"\n|,"ZYY"\n|g' -e 's|,"GMZ"\n|,"FPZ"\n|g' Packs.txt


                I used the entire strings and not just the letters between the double quotes as you didn't specify whether those specific strings appear anywhere else in the file.



                Once you are satisfied with the outcome, you can use the below command to change the file itself.



                sed -i -e 's|," "\n|,"ZYY"\n|g' -e 's|,"ZZY"\n|,"ZYY"\n|g' -e 's|,"GMZ"\n|,"FPZ"\n|g' Packs.txt





                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  This will change all three strings to what you desire and only send the changes to standard output:



                   sed -e 's|," "\n|,"ZYY"\n|g' -e 's|,"ZZY"\n|,"ZYY"\n|g' -e 's|,"GMZ"\n|,"FPZ"\n|g' Packs.txt


                  I used the entire strings and not just the letters between the double quotes as you didn't specify whether those specific strings appear anywhere else in the file.



                  Once you are satisfied with the outcome, you can use the below command to change the file itself.



                  sed -i -e 's|," "\n|,"ZYY"\n|g' -e 's|,"ZZY"\n|,"ZYY"\n|g' -e 's|,"GMZ"\n|,"FPZ"\n|g' Packs.txt





                  share|improve this answer












                  This will change all three strings to what you desire and only send the changes to standard output:



                   sed -e 's|," "\n|,"ZYY"\n|g' -e 's|,"ZZY"\n|,"ZYY"\n|g' -e 's|,"GMZ"\n|,"FPZ"\n|g' Packs.txt


                  I used the entire strings and not just the letters between the double quotes as you didn't specify whether those specific strings appear anywhere else in the file.



                  Once you are satisfied with the outcome, you can use the below command to change the file itself.



                  sed -i -e 's|," "\n|,"ZYY"\n|g' -e 's|,"ZZY"\n|,"ZYY"\n|g' -e 's|,"GMZ"\n|,"FPZ"\n|g' Packs.txt






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 4 hours ago









                  Nasir Riley

                  1,884139




                  1,884139



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f477085%2fsed-to-find-and-replace-regular-expression%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