Replacing a word by another with sed - issue












1















On a redhat server,



I have multiple files named PO*******
Inside them i have a line like this :



NODE PO******* TCPblahblah blah


On the other hand i have a csv file containing multiple line like :



PO*******,XXXXXXX


i want to replace in files PO*******

the NODE PO******* TCPblahblah blah

by
NODE XXXXXX TCPblahblah blah



See my code for what i did to have this output on line with NODE :
TCPblahblXXX



for i in $(ls def)
do
new= $(grep $i list.csv | cut -d ',' -f2)
sed "/NODE/ s/$i/$new/g" def/$i
done









share|improve this question




















  • 1





    What's your current behaviour? I see at least that you haven't specified sed's -i flag, so the result will be output instead of updating the file. You should check whether the output is correct before fixing that of course. You should also avoid parsing the output of ls, for i in ./def/* will work better on edge cases (such as spaces in filenames)

    – Aaron
    Dec 28 '18 at 16:41











  • The formatting in this question makes it really awkward to read.

    – Matias Barrios
    Dec 28 '18 at 16:58











  • * is a special character in sed. For a literal * you have to write *. Also don't use $(ls def) but def/*. You might also want to remove that space after new= .

    – Socowi
    Dec 28 '18 at 19:13













  • Are the asterisks literal?

    – George Vasiliou
    Dec 28 '18 at 20:13
















1















On a redhat server,



I have multiple files named PO*******
Inside them i have a line like this :



NODE PO******* TCPblahblah blah


On the other hand i have a csv file containing multiple line like :



PO*******,XXXXXXX


i want to replace in files PO*******

the NODE PO******* TCPblahblah blah

by
NODE XXXXXX TCPblahblah blah



See my code for what i did to have this output on line with NODE :
TCPblahblXXX



for i in $(ls def)
do
new= $(grep $i list.csv | cut -d ',' -f2)
sed "/NODE/ s/$i/$new/g" def/$i
done









share|improve this question




















  • 1





    What's your current behaviour? I see at least that you haven't specified sed's -i flag, so the result will be output instead of updating the file. You should check whether the output is correct before fixing that of course. You should also avoid parsing the output of ls, for i in ./def/* will work better on edge cases (such as spaces in filenames)

    – Aaron
    Dec 28 '18 at 16:41











  • The formatting in this question makes it really awkward to read.

    – Matias Barrios
    Dec 28 '18 at 16:58











  • * is a special character in sed. For a literal * you have to write *. Also don't use $(ls def) but def/*. You might also want to remove that space after new= .

    – Socowi
    Dec 28 '18 at 19:13













  • Are the asterisks literal?

    – George Vasiliou
    Dec 28 '18 at 20:13














1












1








1








On a redhat server,



I have multiple files named PO*******
Inside them i have a line like this :



NODE PO******* TCPblahblah blah


On the other hand i have a csv file containing multiple line like :



PO*******,XXXXXXX


i want to replace in files PO*******

the NODE PO******* TCPblahblah blah

by
NODE XXXXXX TCPblahblah blah



See my code for what i did to have this output on line with NODE :
TCPblahblXXX



for i in $(ls def)
do
new= $(grep $i list.csv | cut -d ',' -f2)
sed "/NODE/ s/$i/$new/g" def/$i
done









share|improve this question
















On a redhat server,



I have multiple files named PO*******
Inside them i have a line like this :



NODE PO******* TCPblahblah blah


On the other hand i have a csv file containing multiple line like :



PO*******,XXXXXXX


i want to replace in files PO*******

the NODE PO******* TCPblahblah blah

by
NODE XXXXXX TCPblahblah blah



See my code for what i did to have this output on line with NODE :
TCPblahblXXX



for i in $(ls def)
do
new= $(grep $i list.csv | cut -d ',' -f2)
sed "/NODE/ s/$i/$new/g" def/$i
done






bash sed redhat






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 17:23









Tiw

1,482618




1,482618










asked Dec 28 '18 at 16:37









DrateilDrateil

61




61








  • 1





    What's your current behaviour? I see at least that you haven't specified sed's -i flag, so the result will be output instead of updating the file. You should check whether the output is correct before fixing that of course. You should also avoid parsing the output of ls, for i in ./def/* will work better on edge cases (such as spaces in filenames)

    – Aaron
    Dec 28 '18 at 16:41











  • The formatting in this question makes it really awkward to read.

    – Matias Barrios
    Dec 28 '18 at 16:58











  • * is a special character in sed. For a literal * you have to write *. Also don't use $(ls def) but def/*. You might also want to remove that space after new= .

    – Socowi
    Dec 28 '18 at 19:13













  • Are the asterisks literal?

    – George Vasiliou
    Dec 28 '18 at 20:13














  • 1





    What's your current behaviour? I see at least that you haven't specified sed's -i flag, so the result will be output instead of updating the file. You should check whether the output is correct before fixing that of course. You should also avoid parsing the output of ls, for i in ./def/* will work better on edge cases (such as spaces in filenames)

    – Aaron
    Dec 28 '18 at 16:41











  • The formatting in this question makes it really awkward to read.

    – Matias Barrios
    Dec 28 '18 at 16:58











  • * is a special character in sed. For a literal * you have to write *. Also don't use $(ls def) but def/*. You might also want to remove that space after new= .

    – Socowi
    Dec 28 '18 at 19:13













  • Are the asterisks literal?

    – George Vasiliou
    Dec 28 '18 at 20:13








1




1





What's your current behaviour? I see at least that you haven't specified sed's -i flag, so the result will be output instead of updating the file. You should check whether the output is correct before fixing that of course. You should also avoid parsing the output of ls, for i in ./def/* will work better on edge cases (such as spaces in filenames)

– Aaron
Dec 28 '18 at 16:41





What's your current behaviour? I see at least that you haven't specified sed's -i flag, so the result will be output instead of updating the file. You should check whether the output is correct before fixing that of course. You should also avoid parsing the output of ls, for i in ./def/* will work better on edge cases (such as spaces in filenames)

– Aaron
Dec 28 '18 at 16:41













The formatting in this question makes it really awkward to read.

– Matias Barrios
Dec 28 '18 at 16:58





The formatting in this question makes it really awkward to read.

– Matias Barrios
Dec 28 '18 at 16:58













* is a special character in sed. For a literal * you have to write *. Also don't use $(ls def) but def/*. You might also want to remove that space after new= .

– Socowi
Dec 28 '18 at 19:13







* is a special character in sed. For a literal * you have to write *. Also don't use $(ls def) but def/*. You might also want to remove that space after new= .

– Socowi
Dec 28 '18 at 19:13















Are the asterisks literal?

– George Vasiliou
Dec 28 '18 at 20:13





Are the asterisks literal?

– George Vasiliou
Dec 28 '18 at 20:13












2 Answers
2






active

oldest

votes


















2














When each def/* file only has one /NODE/ line, you can try a nested sed.

Remember that sed can use other characters like , or # when you don't want the /.



# First look at the generated commands by
sed 's#.*#/NODE/ s,&,#' list.csv
# next try
sed -f <(sed 's#.*#/NODE/ s,&,#' list.csv) def/*
# Make backup before changing
cp -r def def_bak
# Get serious
sed -if <(sed 's#.*#/NODE/ s,&,#' list.csv) def/*


When you are not sure about this (overlapping files like PO1 and PO12), you can loop through the files, starting with your csv.



while IFS= read -f line; do
test -f def/"${line%,*}" || continue
sed -i "s,${line}," def/"${line%,*}"
done < list.csv





share|improve this answer































    0














    you have too much blah blah and lacking testable input files, but I think this will do



    $ awk 'NR==FNR{a[$1]=$2; next} {f=FILENAME; sub(f,a[f]); print > f".mod"}' list.csv def/*


    this will create ".mod" extensions for the processed files.






    share|improve this answer























      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%2f53961592%2freplacing-a-word-by-another-with-sed-issue%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









      2














      When each def/* file only has one /NODE/ line, you can try a nested sed.

      Remember that sed can use other characters like , or # when you don't want the /.



      # First look at the generated commands by
      sed 's#.*#/NODE/ s,&,#' list.csv
      # next try
      sed -f <(sed 's#.*#/NODE/ s,&,#' list.csv) def/*
      # Make backup before changing
      cp -r def def_bak
      # Get serious
      sed -if <(sed 's#.*#/NODE/ s,&,#' list.csv) def/*


      When you are not sure about this (overlapping files like PO1 and PO12), you can loop through the files, starting with your csv.



      while IFS= read -f line; do
      test -f def/"${line%,*}" || continue
      sed -i "s,${line}," def/"${line%,*}"
      done < list.csv





      share|improve this answer




























        2














        When each def/* file only has one /NODE/ line, you can try a nested sed.

        Remember that sed can use other characters like , or # when you don't want the /.



        # First look at the generated commands by
        sed 's#.*#/NODE/ s,&,#' list.csv
        # next try
        sed -f <(sed 's#.*#/NODE/ s,&,#' list.csv) def/*
        # Make backup before changing
        cp -r def def_bak
        # Get serious
        sed -if <(sed 's#.*#/NODE/ s,&,#' list.csv) def/*


        When you are not sure about this (overlapping files like PO1 and PO12), you can loop through the files, starting with your csv.



        while IFS= read -f line; do
        test -f def/"${line%,*}" || continue
        sed -i "s,${line}," def/"${line%,*}"
        done < list.csv





        share|improve this answer


























          2












          2








          2







          When each def/* file only has one /NODE/ line, you can try a nested sed.

          Remember that sed can use other characters like , or # when you don't want the /.



          # First look at the generated commands by
          sed 's#.*#/NODE/ s,&,#' list.csv
          # next try
          sed -f <(sed 's#.*#/NODE/ s,&,#' list.csv) def/*
          # Make backup before changing
          cp -r def def_bak
          # Get serious
          sed -if <(sed 's#.*#/NODE/ s,&,#' list.csv) def/*


          When you are not sure about this (overlapping files like PO1 and PO12), you can loop through the files, starting with your csv.



          while IFS= read -f line; do
          test -f def/"${line%,*}" || continue
          sed -i "s,${line}," def/"${line%,*}"
          done < list.csv





          share|improve this answer













          When each def/* file only has one /NODE/ line, you can try a nested sed.

          Remember that sed can use other characters like , or # when you don't want the /.



          # First look at the generated commands by
          sed 's#.*#/NODE/ s,&,#' list.csv
          # next try
          sed -f <(sed 's#.*#/NODE/ s,&,#' list.csv) def/*
          # Make backup before changing
          cp -r def def_bak
          # Get serious
          sed -if <(sed 's#.*#/NODE/ s,&,#' list.csv) def/*


          When you are not sure about this (overlapping files like PO1 and PO12), you can loop through the files, starting with your csv.



          while IFS= read -f line; do
          test -f def/"${line%,*}" || continue
          sed -i "s,${line}," def/"${line%,*}"
          done < list.csv






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 28 '18 at 21:15









          Walter AWalter A

          10.5k2931




          10.5k2931

























              0














              you have too much blah blah and lacking testable input files, but I think this will do



              $ awk 'NR==FNR{a[$1]=$2; next} {f=FILENAME; sub(f,a[f]); print > f".mod"}' list.csv def/*


              this will create ".mod" extensions for the processed files.






              share|improve this answer




























                0














                you have too much blah blah and lacking testable input files, but I think this will do



                $ awk 'NR==FNR{a[$1]=$2; next} {f=FILENAME; sub(f,a[f]); print > f".mod"}' list.csv def/*


                this will create ".mod" extensions for the processed files.






                share|improve this answer


























                  0












                  0








                  0







                  you have too much blah blah and lacking testable input files, but I think this will do



                  $ awk 'NR==FNR{a[$1]=$2; next} {f=FILENAME; sub(f,a[f]); print > f".mod"}' list.csv def/*


                  this will create ".mod" extensions for the processed files.






                  share|improve this answer













                  you have too much blah blah and lacking testable input files, but I think this will do



                  $ awk 'NR==FNR{a[$1]=$2; next} {f=FILENAME; sub(f,a[f]); print > f".mod"}' list.csv def/*


                  this will create ".mod" extensions for the processed files.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 28 '18 at 17:04









                  karakfakarakfa

                  48.4k52738




                  48.4k52738






























                      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%2f53961592%2freplacing-a-word-by-another-with-sed-issue%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

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas