Use a command inside sed

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











up vote
1
down vote

favorite












The purpose of the command should be replacing any IP in file myfile.txt with the local ip of the server automatically



example:



sed -i -e 's/[0-9]1,3.[0-9]1,3.[0-9]1,3.[0-9]1,3/LOCALIP/g' myfile.txt


i want to know if it possible to use command for getting local ip address inside sed



basically i want to replace LOCALIP in my example with this command



/sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1 









share|improve this question









New contributor




Ali EL KANDOUSSI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    1
    down vote

    favorite












    The purpose of the command should be replacing any IP in file myfile.txt with the local ip of the server automatically



    example:



    sed -i -e 's/[0-9]1,3.[0-9]1,3.[0-9]1,3.[0-9]1,3/LOCALIP/g' myfile.txt


    i want to know if it possible to use command for getting local ip address inside sed



    basically i want to replace LOCALIP in my example with this command



    /sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1 









    share|improve this question









    New contributor




    Ali EL KANDOUSSI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      The purpose of the command should be replacing any IP in file myfile.txt with the local ip of the server automatically



      example:



      sed -i -e 's/[0-9]1,3.[0-9]1,3.[0-9]1,3.[0-9]1,3/LOCALIP/g' myfile.txt


      i want to know if it possible to use command for getting local ip address inside sed



      basically i want to replace LOCALIP in my example with this command



      /sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1 









      share|improve this question









      New contributor




      Ali EL KANDOUSSI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      The purpose of the command should be replacing any IP in file myfile.txt with the local ip of the server automatically



      example:



      sed -i -e 's/[0-9]1,3.[0-9]1,3.[0-9]1,3.[0-9]1,3/LOCALIP/g' myfile.txt


      i want to know if it possible to use command for getting local ip address inside sed



      basically i want to replace LOCALIP in my example with this command



      /sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1 






      linux sed






      share|improve this question









      New contributor




      Ali EL KANDOUSSI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Ali EL KANDOUSSI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited Nov 21 at 14:52









      Arcticooling

      1




      1






      New contributor




      Ali EL KANDOUSSI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 21 at 14:46









      Ali EL KANDOUSSI

      54




      54




      New contributor




      Ali EL KANDOUSSI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Ali EL KANDOUSSI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Ali EL KANDOUSSI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote













          You can use command substitution in double quotes:



          sed -i -e 's/[0-9]1,3.[0-9]1,3.[0-9]1,3.[0-9]1,3/'"$(/sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1)/g" myfile.txt


          In single quotes, this won't work, so end the single quotes before LOCALIP, use double quotes for the command. This way, we don't accidentally do expansions in the search pattern.






          share|improve this answer








          New contributor




          Arcticooling is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.

















          • Thank you for quick answer, got this error : sed: -e expression #1, char 71: unterminated `s' command
            – Ali EL KANDOUSSI
            Nov 21 at 14:54











          • Are you sure the command to get IP is executing correctly? What output do you get with /sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1)? Also, try putting an echo before sed to see what sed command is finally run.
            – Arcticooling
            Nov 21 at 15:02










          • Got this output with /sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1): 207.154.202.52 10.19.0.6
            – Ali EL KANDOUSSI
            Nov 21 at 15:12










          • @JeffSchaller but "$(...)"/g and "$(...)/g" should both give the same string, shouldn't it?
            – Arcticooling
            Nov 21 at 15:27






          • 1




            Appears the OP has a space - separated list of two IP's coming back from that command, causing the sed command to end early.
            – Jeff Schaller
            Nov 21 at 16:12

















          up vote
          0
          down vote













          Below command worked fine for me



          sed "s/[0-9]1,3.[0-9]1,3.[0-9]1,3.[0-9]1,3/$(/sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1)/"





          share|improve this answer





























            up vote
            0
            down vote



            accepted










            i solved my issue with this command



            sed -i "s/[0-9]1,3.[0-9]1,3.[0-9]1,3.[0-9]1,3/$(ifconfig | grep 'inet ' | grep -v '127.0.0.1' | awk 'print $2')/" myfile.txt





            share|improve this answer








            New contributor




            Ali EL KANDOUSSI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.

















              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: 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
              );



              );






              Ali EL KANDOUSSI is a new contributor. Be nice, and check out our Code of Conduct.









               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f483226%2fuse-a-command-inside-sed%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote













              You can use command substitution in double quotes:



              sed -i -e 's/[0-9]1,3.[0-9]1,3.[0-9]1,3.[0-9]1,3/'"$(/sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1)/g" myfile.txt


              In single quotes, this won't work, so end the single quotes before LOCALIP, use double quotes for the command. This way, we don't accidentally do expansions in the search pattern.






              share|improve this answer








              New contributor




              Arcticooling is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.

















              • Thank you for quick answer, got this error : sed: -e expression #1, char 71: unterminated `s' command
                – Ali EL KANDOUSSI
                Nov 21 at 14:54











              • Are you sure the command to get IP is executing correctly? What output do you get with /sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1)? Also, try putting an echo before sed to see what sed command is finally run.
                – Arcticooling
                Nov 21 at 15:02










              • Got this output with /sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1): 207.154.202.52 10.19.0.6
                – Ali EL KANDOUSSI
                Nov 21 at 15:12










              • @JeffSchaller but "$(...)"/g and "$(...)/g" should both give the same string, shouldn't it?
                – Arcticooling
                Nov 21 at 15:27






              • 1




                Appears the OP has a space - separated list of two IP's coming back from that command, causing the sed command to end early.
                – Jeff Schaller
                Nov 21 at 16:12














              up vote
              1
              down vote













              You can use command substitution in double quotes:



              sed -i -e 's/[0-9]1,3.[0-9]1,3.[0-9]1,3.[0-9]1,3/'"$(/sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1)/g" myfile.txt


              In single quotes, this won't work, so end the single quotes before LOCALIP, use double quotes for the command. This way, we don't accidentally do expansions in the search pattern.






              share|improve this answer








              New contributor




              Arcticooling is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.

















              • Thank you for quick answer, got this error : sed: -e expression #1, char 71: unterminated `s' command
                – Ali EL KANDOUSSI
                Nov 21 at 14:54











              • Are you sure the command to get IP is executing correctly? What output do you get with /sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1)? Also, try putting an echo before sed to see what sed command is finally run.
                – Arcticooling
                Nov 21 at 15:02










              • Got this output with /sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1): 207.154.202.52 10.19.0.6
                – Ali EL KANDOUSSI
                Nov 21 at 15:12










              • @JeffSchaller but "$(...)"/g and "$(...)/g" should both give the same string, shouldn't it?
                – Arcticooling
                Nov 21 at 15:27






              • 1




                Appears the OP has a space - separated list of two IP's coming back from that command, causing the sed command to end early.
                – Jeff Schaller
                Nov 21 at 16:12












              up vote
              1
              down vote










              up vote
              1
              down vote









              You can use command substitution in double quotes:



              sed -i -e 's/[0-9]1,3.[0-9]1,3.[0-9]1,3.[0-9]1,3/'"$(/sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1)/g" myfile.txt


              In single quotes, this won't work, so end the single quotes before LOCALIP, use double quotes for the command. This way, we don't accidentally do expansions in the search pattern.






              share|improve this answer








              New contributor




              Arcticooling is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.









              You can use command substitution in double quotes:



              sed -i -e 's/[0-9]1,3.[0-9]1,3.[0-9]1,3.[0-9]1,3/'"$(/sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1)/g" myfile.txt


              In single quotes, this won't work, so end the single quotes before LOCALIP, use double quotes for the command. This way, we don't accidentally do expansions in the search pattern.







              share|improve this answer








              New contributor




              Arcticooling is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.









              share|improve this answer



              share|improve this answer






              New contributor




              Arcticooling is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.









              answered Nov 21 at 14:52









              Arcticooling

              1




              1




              New contributor




              Arcticooling is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.





              New contributor





              Arcticooling is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






              Arcticooling is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.











              • Thank you for quick answer, got this error : sed: -e expression #1, char 71: unterminated `s' command
                – Ali EL KANDOUSSI
                Nov 21 at 14:54











              • Are you sure the command to get IP is executing correctly? What output do you get with /sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1)? Also, try putting an echo before sed to see what sed command is finally run.
                – Arcticooling
                Nov 21 at 15:02










              • Got this output with /sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1): 207.154.202.52 10.19.0.6
                – Ali EL KANDOUSSI
                Nov 21 at 15:12










              • @JeffSchaller but "$(...)"/g and "$(...)/g" should both give the same string, shouldn't it?
                – Arcticooling
                Nov 21 at 15:27






              • 1




                Appears the OP has a space - separated list of two IP's coming back from that command, causing the sed command to end early.
                – Jeff Schaller
                Nov 21 at 16:12
















              • Thank you for quick answer, got this error : sed: -e expression #1, char 71: unterminated `s' command
                – Ali EL KANDOUSSI
                Nov 21 at 14:54











              • Are you sure the command to get IP is executing correctly? What output do you get with /sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1)? Also, try putting an echo before sed to see what sed command is finally run.
                – Arcticooling
                Nov 21 at 15:02










              • Got this output with /sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1): 207.154.202.52 10.19.0.6
                – Ali EL KANDOUSSI
                Nov 21 at 15:12










              • @JeffSchaller but "$(...)"/g and "$(...)/g" should both give the same string, shouldn't it?
                – Arcticooling
                Nov 21 at 15:27






              • 1




                Appears the OP has a space - separated list of two IP's coming back from that command, causing the sed command to end early.
                – Jeff Schaller
                Nov 21 at 16:12















              Thank you for quick answer, got this error : sed: -e expression #1, char 71: unterminated `s' command
              – Ali EL KANDOUSSI
              Nov 21 at 14:54





              Thank you for quick answer, got this error : sed: -e expression #1, char 71: unterminated `s' command
              – Ali EL KANDOUSSI
              Nov 21 at 14:54













              Are you sure the command to get IP is executing correctly? What output do you get with /sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1)? Also, try putting an echo before sed to see what sed command is finally run.
              – Arcticooling
              Nov 21 at 15:02




              Are you sure the command to get IP is executing correctly? What output do you get with /sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1)? Also, try putting an echo before sed to see what sed command is finally run.
              – Arcticooling
              Nov 21 at 15:02












              Got this output with /sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1): 207.154.202.52 10.19.0.6
              – Ali EL KANDOUSSI
              Nov 21 at 15:12




              Got this output with /sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1): 207.154.202.52 10.19.0.6
              – Ali EL KANDOUSSI
              Nov 21 at 15:12












              @JeffSchaller but "$(...)"/g and "$(...)/g" should both give the same string, shouldn't it?
              – Arcticooling
              Nov 21 at 15:27




              @JeffSchaller but "$(...)"/g and "$(...)/g" should both give the same string, shouldn't it?
              – Arcticooling
              Nov 21 at 15:27




              1




              1




              Appears the OP has a space - separated list of two IP's coming back from that command, causing the sed command to end early.
              – Jeff Schaller
              Nov 21 at 16:12




              Appears the OP has a space - separated list of two IP's coming back from that command, causing the sed command to end early.
              – Jeff Schaller
              Nov 21 at 16:12












              up vote
              0
              down vote













              Below command worked fine for me



              sed "s/[0-9]1,3.[0-9]1,3.[0-9]1,3.[0-9]1,3/$(/sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1)/"





              share|improve this answer


























                up vote
                0
                down vote













                Below command worked fine for me



                sed "s/[0-9]1,3.[0-9]1,3.[0-9]1,3.[0-9]1,3/$(/sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1)/"





                share|improve this answer
























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Below command worked fine for me



                  sed "s/[0-9]1,3.[0-9]1,3.[0-9]1,3.[0-9]1,3/$(/sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1)/"





                  share|improve this answer














                  Below command worked fine for me



                  sed "s/[0-9]1,3.[0-9]1,3.[0-9]1,3.[0-9]1,3/$(/sbin/ip -o -4 addr list eth0 | awk 'print $4' | cut -d/ -f1)/"






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 21 at 16:11









                  Jesse_b

                  11.4k23063




                  11.4k23063










                  answered Nov 21 at 15:30









                  Praveen Kumar BS

                  1,114138




                  1,114138




















                      up vote
                      0
                      down vote



                      accepted










                      i solved my issue with this command



                      sed -i "s/[0-9]1,3.[0-9]1,3.[0-9]1,3.[0-9]1,3/$(ifconfig | grep 'inet ' | grep -v '127.0.0.1' | awk 'print $2')/" myfile.txt





                      share|improve this answer








                      New contributor




                      Ali EL KANDOUSSI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.





















                        up vote
                        0
                        down vote



                        accepted










                        i solved my issue with this command



                        sed -i "s/[0-9]1,3.[0-9]1,3.[0-9]1,3.[0-9]1,3/$(ifconfig | grep 'inet ' | grep -v '127.0.0.1' | awk 'print $2')/" myfile.txt





                        share|improve this answer








                        New contributor




                        Ali EL KANDOUSSI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.



















                          up vote
                          0
                          down vote



                          accepted







                          up vote
                          0
                          down vote



                          accepted






                          i solved my issue with this command



                          sed -i "s/[0-9]1,3.[0-9]1,3.[0-9]1,3.[0-9]1,3/$(ifconfig | grep 'inet ' | grep -v '127.0.0.1' | awk 'print $2')/" myfile.txt





                          share|improve this answer








                          New contributor




                          Ali EL KANDOUSSI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          i solved my issue with this command



                          sed -i "s/[0-9]1,3.[0-9]1,3.[0-9]1,3.[0-9]1,3/$(ifconfig | grep 'inet ' | grep -v '127.0.0.1' | awk 'print $2')/" myfile.txt






                          share|improve this answer








                          New contributor




                          Ali EL KANDOUSSI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          share|improve this answer



                          share|improve this answer






                          New contributor




                          Ali EL KANDOUSSI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          answered Nov 21 at 17:28









                          Ali EL KANDOUSSI

                          54




                          54




                          New contributor




                          Ali EL KANDOUSSI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.





                          New contributor





                          Ali EL KANDOUSSI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






                          Ali EL KANDOUSSI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.




















                              Ali EL KANDOUSSI is a new contributor. Be nice, and check out our Code of Conduct.









                               

                              draft saved


                              draft discarded


















                              Ali EL KANDOUSSI is a new contributor. Be nice, and check out our Code of Conduct.












                              Ali EL KANDOUSSI is a new contributor. Be nice, and check out our Code of Conduct.











                              Ali EL KANDOUSSI is a new contributor. Be nice, and check out our Code of Conduct.













                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f483226%2fuse-a-command-inside-sed%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

                              How to check contact read email or not when send email to Individual?

                              Displaying single band from multi-band raster using QGIS

                              How many registers does an x86_64 CPU actually have?