sed replace issue

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











up vote
0
down vote

favorite












My file contains



$Param_T=ABC49_SA_T2
$Param_V=ABC49BC_SA_V2
$Param_ST=ABC491_SA_M2


I am trying to replace a value starts with ABC*_ with XYZ12_



sed -i 's/ABC.*_/XYZ12_/g' INCR.parm


Above is the sed command i have used. It is not throwing any error but value is not changing.Could someone please help me with this ?







share|improve this question

























    up vote
    0
    down vote

    favorite












    My file contains



    $Param_T=ABC49_SA_T2
    $Param_V=ABC49BC_SA_V2
    $Param_ST=ABC491_SA_M2


    I am trying to replace a value starts with ABC*_ with XYZ12_



    sed -i 's/ABC.*_/XYZ12_/g' INCR.parm


    Above is the sed command i have used. It is not throwing any error but value is not changing.Could someone please help me with this ?







    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      My file contains



      $Param_T=ABC49_SA_T2
      $Param_V=ABC49BC_SA_V2
      $Param_ST=ABC491_SA_M2


      I am trying to replace a value starts with ABC*_ with XYZ12_



      sed -i 's/ABC.*_/XYZ12_/g' INCR.parm


      Above is the sed command i have used. It is not throwing any error but value is not changing.Could someone please help me with this ?







      share|improve this question













      My file contains



      $Param_T=ABC49_SA_T2
      $Param_V=ABC49BC_SA_V2
      $Param_ST=ABC491_SA_M2


      I am trying to replace a value starts with ABC*_ with XYZ12_



      sed -i 's/ABC.*_/XYZ12_/g' INCR.parm


      Above is the sed command i have used. It is not throwing any error but value is not changing.Could someone please help me with this ?









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 28 at 13:16









      glenn jackman

      45.6k264100




      45.6k264100









      asked Jun 28 at 10:45









      user_297020

      116




      116




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          With sed, the way to implement non-greedy matching is to specify a set of character you don't want to match:



          sed -i 's/=ABC[^_]*_/=XYZ12_/g' INCR.parm


          That is: match "ABC" followed by zero or more non-underscore characters followed by underscore.



          And note, you don't escape the dot. A "bare" dot means "match any character"; an escaped dot means "match a literal dot". You weren't making any replacements because none of the data matches the literal string "ABC."






          share|improve this answer



















          • 1




            You might want to match the = as well, or the start of the word ABC in some other way (<). Otherwise they will do weird things to lines like ...=BOOABC09_....
            – Kusalananda
            Jun 28 at 13:20










          • Good point, thanks
            – glenn jackman
            Jun 28 at 13:26

















          up vote
          0
          down vote













          Dont escape the "." that matches all characters [ Greedy]



          sed -i 's/ABC.*_/XYZ12_/g' INCR.parm


          Output:



          $Param_T=XYZ12_T2
          $Param_V=XYZ12_V2
          $Param_ST=XYZ12_M2)


          If you want to replace ABC* only uptill first underscore ( _ ) [Lazy], you need to use perl regex



          use



          perl -pe 's/ABC(.*?)_/XYZ12_/g' INCR.parm


          Output:



          $Param_T=XYZ12_SA_T2
          $Param_V=XYZ12_SA_V2
          $Param_ST=XYZ12_T2



          'Greedy' means match longest possible string.



          'Lazy' means match shortest possible string.







          share|improve this answer























          • Thanks for this..It worked. But have some issue when there is a tab or space before $Param* (sample " $Param_T=ABC12_T2") . Value is not getting replaced.
            – user_297020
            Jun 28 at 11:02










          • I dont think so it should be a problem, can you update the question with the exact values you need to get replaced , i tried with your sample and its working for me
            – Arushix
            Jun 28 at 11:18










          • " $Param_STG_V=DTST49_SA_STG_RKEM2_V" " $Param_SIM_T=DTST49_SA_SIM_RKEM2" " $Param_SIM_V=DTST49_SA_SIM_RKEM2_V" " $Param_WRK_T=DTST49_TMP_LOAD_UTILITY" Above is my sample data, I have to replace with DTST49 with DDEV25,
            – user_297020
            Jun 28 at 11:23











          • have added quotes as it is not accepting space or tab before
            – user_297020
            Jun 28 at 11:24










          • urplease edit the question and add your sample, as it is not very clear
            – Arushix
            Jun 28 at 12:05










          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%2f452405%2fsed-replace-issue%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
          1
          down vote













          With sed, the way to implement non-greedy matching is to specify a set of character you don't want to match:



          sed -i 's/=ABC[^_]*_/=XYZ12_/g' INCR.parm


          That is: match "ABC" followed by zero or more non-underscore characters followed by underscore.



          And note, you don't escape the dot. A "bare" dot means "match any character"; an escaped dot means "match a literal dot". You weren't making any replacements because none of the data matches the literal string "ABC."






          share|improve this answer



















          • 1




            You might want to match the = as well, or the start of the word ABC in some other way (<). Otherwise they will do weird things to lines like ...=BOOABC09_....
            – Kusalananda
            Jun 28 at 13:20










          • Good point, thanks
            – glenn jackman
            Jun 28 at 13:26














          up vote
          1
          down vote













          With sed, the way to implement non-greedy matching is to specify a set of character you don't want to match:



          sed -i 's/=ABC[^_]*_/=XYZ12_/g' INCR.parm


          That is: match "ABC" followed by zero or more non-underscore characters followed by underscore.



          And note, you don't escape the dot. A "bare" dot means "match any character"; an escaped dot means "match a literal dot". You weren't making any replacements because none of the data matches the literal string "ABC."






          share|improve this answer



















          • 1




            You might want to match the = as well, or the start of the word ABC in some other way (<). Otherwise they will do weird things to lines like ...=BOOABC09_....
            – Kusalananda
            Jun 28 at 13:20










          • Good point, thanks
            – glenn jackman
            Jun 28 at 13:26












          up vote
          1
          down vote










          up vote
          1
          down vote









          With sed, the way to implement non-greedy matching is to specify a set of character you don't want to match:



          sed -i 's/=ABC[^_]*_/=XYZ12_/g' INCR.parm


          That is: match "ABC" followed by zero or more non-underscore characters followed by underscore.



          And note, you don't escape the dot. A "bare" dot means "match any character"; an escaped dot means "match a literal dot". You weren't making any replacements because none of the data matches the literal string "ABC."






          share|improve this answer















          With sed, the way to implement non-greedy matching is to specify a set of character you don't want to match:



          sed -i 's/=ABC[^_]*_/=XYZ12_/g' INCR.parm


          That is: match "ABC" followed by zero or more non-underscore characters followed by underscore.



          And note, you don't escape the dot. A "bare" dot means "match any character"; an escaped dot means "match a literal dot". You weren't making any replacements because none of the data matches the literal string "ABC."







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Jun 28 at 13:26


























          answered Jun 28 at 13:15









          glenn jackman

          45.6k264100




          45.6k264100







          • 1




            You might want to match the = as well, or the start of the word ABC in some other way (<). Otherwise they will do weird things to lines like ...=BOOABC09_....
            – Kusalananda
            Jun 28 at 13:20










          • Good point, thanks
            – glenn jackman
            Jun 28 at 13:26












          • 1




            You might want to match the = as well, or the start of the word ABC in some other way (<). Otherwise they will do weird things to lines like ...=BOOABC09_....
            – Kusalananda
            Jun 28 at 13:20










          • Good point, thanks
            – glenn jackman
            Jun 28 at 13:26







          1




          1




          You might want to match the = as well, or the start of the word ABC in some other way (<). Otherwise they will do weird things to lines like ...=BOOABC09_....
          – Kusalananda
          Jun 28 at 13:20




          You might want to match the = as well, or the start of the word ABC in some other way (<). Otherwise they will do weird things to lines like ...=BOOABC09_....
          – Kusalananda
          Jun 28 at 13:20












          Good point, thanks
          – glenn jackman
          Jun 28 at 13:26




          Good point, thanks
          – glenn jackman
          Jun 28 at 13:26












          up vote
          0
          down vote













          Dont escape the "." that matches all characters [ Greedy]



          sed -i 's/ABC.*_/XYZ12_/g' INCR.parm


          Output:



          $Param_T=XYZ12_T2
          $Param_V=XYZ12_V2
          $Param_ST=XYZ12_M2)


          If you want to replace ABC* only uptill first underscore ( _ ) [Lazy], you need to use perl regex



          use



          perl -pe 's/ABC(.*?)_/XYZ12_/g' INCR.parm


          Output:



          $Param_T=XYZ12_SA_T2
          $Param_V=XYZ12_SA_V2
          $Param_ST=XYZ12_T2



          'Greedy' means match longest possible string.



          'Lazy' means match shortest possible string.







          share|improve this answer























          • Thanks for this..It worked. But have some issue when there is a tab or space before $Param* (sample " $Param_T=ABC12_T2") . Value is not getting replaced.
            – user_297020
            Jun 28 at 11:02










          • I dont think so it should be a problem, can you update the question with the exact values you need to get replaced , i tried with your sample and its working for me
            – Arushix
            Jun 28 at 11:18










          • " $Param_STG_V=DTST49_SA_STG_RKEM2_V" " $Param_SIM_T=DTST49_SA_SIM_RKEM2" " $Param_SIM_V=DTST49_SA_SIM_RKEM2_V" " $Param_WRK_T=DTST49_TMP_LOAD_UTILITY" Above is my sample data, I have to replace with DTST49 with DDEV25,
            – user_297020
            Jun 28 at 11:23











          • have added quotes as it is not accepting space or tab before
            – user_297020
            Jun 28 at 11:24










          • urplease edit the question and add your sample, as it is not very clear
            – Arushix
            Jun 28 at 12:05














          up vote
          0
          down vote













          Dont escape the "." that matches all characters [ Greedy]



          sed -i 's/ABC.*_/XYZ12_/g' INCR.parm


          Output:



          $Param_T=XYZ12_T2
          $Param_V=XYZ12_V2
          $Param_ST=XYZ12_M2)


          If you want to replace ABC* only uptill first underscore ( _ ) [Lazy], you need to use perl regex



          use



          perl -pe 's/ABC(.*?)_/XYZ12_/g' INCR.parm


          Output:



          $Param_T=XYZ12_SA_T2
          $Param_V=XYZ12_SA_V2
          $Param_ST=XYZ12_T2



          'Greedy' means match longest possible string.



          'Lazy' means match shortest possible string.







          share|improve this answer























          • Thanks for this..It worked. But have some issue when there is a tab or space before $Param* (sample " $Param_T=ABC12_T2") . Value is not getting replaced.
            – user_297020
            Jun 28 at 11:02










          • I dont think so it should be a problem, can you update the question with the exact values you need to get replaced , i tried with your sample and its working for me
            – Arushix
            Jun 28 at 11:18










          • " $Param_STG_V=DTST49_SA_STG_RKEM2_V" " $Param_SIM_T=DTST49_SA_SIM_RKEM2" " $Param_SIM_V=DTST49_SA_SIM_RKEM2_V" " $Param_WRK_T=DTST49_TMP_LOAD_UTILITY" Above is my sample data, I have to replace with DTST49 with DDEV25,
            – user_297020
            Jun 28 at 11:23











          • have added quotes as it is not accepting space or tab before
            – user_297020
            Jun 28 at 11:24










          • urplease edit the question and add your sample, as it is not very clear
            – Arushix
            Jun 28 at 12:05












          up vote
          0
          down vote










          up vote
          0
          down vote









          Dont escape the "." that matches all characters [ Greedy]



          sed -i 's/ABC.*_/XYZ12_/g' INCR.parm


          Output:



          $Param_T=XYZ12_T2
          $Param_V=XYZ12_V2
          $Param_ST=XYZ12_M2)


          If you want to replace ABC* only uptill first underscore ( _ ) [Lazy], you need to use perl regex



          use



          perl -pe 's/ABC(.*?)_/XYZ12_/g' INCR.parm


          Output:



          $Param_T=XYZ12_SA_T2
          $Param_V=XYZ12_SA_V2
          $Param_ST=XYZ12_T2



          'Greedy' means match longest possible string.



          'Lazy' means match shortest possible string.







          share|improve this answer















          Dont escape the "." that matches all characters [ Greedy]



          sed -i 's/ABC.*_/XYZ12_/g' INCR.parm


          Output:



          $Param_T=XYZ12_T2
          $Param_V=XYZ12_V2
          $Param_ST=XYZ12_M2)


          If you want to replace ABC* only uptill first underscore ( _ ) [Lazy], you need to use perl regex



          use



          perl -pe 's/ABC(.*?)_/XYZ12_/g' INCR.parm


          Output:



          $Param_T=XYZ12_SA_T2
          $Param_V=XYZ12_SA_V2
          $Param_ST=XYZ12_T2



          'Greedy' means match longest possible string.



          'Lazy' means match shortest possible string.








          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Jun 28 at 11:16


























          answered Jun 28 at 10:50









          Arushix

          9968




          9968











          • Thanks for this..It worked. But have some issue when there is a tab or space before $Param* (sample " $Param_T=ABC12_T2") . Value is not getting replaced.
            – user_297020
            Jun 28 at 11:02










          • I dont think so it should be a problem, can you update the question with the exact values you need to get replaced , i tried with your sample and its working for me
            – Arushix
            Jun 28 at 11:18










          • " $Param_STG_V=DTST49_SA_STG_RKEM2_V" " $Param_SIM_T=DTST49_SA_SIM_RKEM2" " $Param_SIM_V=DTST49_SA_SIM_RKEM2_V" " $Param_WRK_T=DTST49_TMP_LOAD_UTILITY" Above is my sample data, I have to replace with DTST49 with DDEV25,
            – user_297020
            Jun 28 at 11:23











          • have added quotes as it is not accepting space or tab before
            – user_297020
            Jun 28 at 11:24










          • urplease edit the question and add your sample, as it is not very clear
            – Arushix
            Jun 28 at 12:05
















          • Thanks for this..It worked. But have some issue when there is a tab or space before $Param* (sample " $Param_T=ABC12_T2") . Value is not getting replaced.
            – user_297020
            Jun 28 at 11:02










          • I dont think so it should be a problem, can you update the question with the exact values you need to get replaced , i tried with your sample and its working for me
            – Arushix
            Jun 28 at 11:18










          • " $Param_STG_V=DTST49_SA_STG_RKEM2_V" " $Param_SIM_T=DTST49_SA_SIM_RKEM2" " $Param_SIM_V=DTST49_SA_SIM_RKEM2_V" " $Param_WRK_T=DTST49_TMP_LOAD_UTILITY" Above is my sample data, I have to replace with DTST49 with DDEV25,
            – user_297020
            Jun 28 at 11:23











          • have added quotes as it is not accepting space or tab before
            – user_297020
            Jun 28 at 11:24










          • urplease edit the question and add your sample, as it is not very clear
            – Arushix
            Jun 28 at 12:05















          Thanks for this..It worked. But have some issue when there is a tab or space before $Param* (sample " $Param_T=ABC12_T2") . Value is not getting replaced.
          – user_297020
          Jun 28 at 11:02




          Thanks for this..It worked. But have some issue when there is a tab or space before $Param* (sample " $Param_T=ABC12_T2") . Value is not getting replaced.
          – user_297020
          Jun 28 at 11:02












          I dont think so it should be a problem, can you update the question with the exact values you need to get replaced , i tried with your sample and its working for me
          – Arushix
          Jun 28 at 11:18




          I dont think so it should be a problem, can you update the question with the exact values you need to get replaced , i tried with your sample and its working for me
          – Arushix
          Jun 28 at 11:18












          " $Param_STG_V=DTST49_SA_STG_RKEM2_V" " $Param_SIM_T=DTST49_SA_SIM_RKEM2" " $Param_SIM_V=DTST49_SA_SIM_RKEM2_V" " $Param_WRK_T=DTST49_TMP_LOAD_UTILITY" Above is my sample data, I have to replace with DTST49 with DDEV25,
          – user_297020
          Jun 28 at 11:23





          " $Param_STG_V=DTST49_SA_STG_RKEM2_V" " $Param_SIM_T=DTST49_SA_SIM_RKEM2" " $Param_SIM_V=DTST49_SA_SIM_RKEM2_V" " $Param_WRK_T=DTST49_TMP_LOAD_UTILITY" Above is my sample data, I have to replace with DTST49 with DDEV25,
          – user_297020
          Jun 28 at 11:23













          have added quotes as it is not accepting space or tab before
          – user_297020
          Jun 28 at 11:24




          have added quotes as it is not accepting space or tab before
          – user_297020
          Jun 28 at 11:24












          urplease edit the question and add your sample, as it is not very clear
          – Arushix
          Jun 28 at 12:05




          urplease edit the question and add your sample, as it is not very clear
          – Arushix
          Jun 28 at 12:05












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f452405%2fsed-replace-issue%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Peggy Mitchell

          Palaiologos

          The Forum (Inglewood, California)