sed - replace string with file contents











up vote
18
down vote

favorite
1












I have two files: file1 and file2.



file1 has the following contents:



---
host: "localhost"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "localhost:2181"


file2 contains an IP address (1.1.1.1)



What I want to do is replace localhost with 1.1.1.1, so that the end result is:



---
host: "1.1.1.1"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "1.1.1.1:2181"


I have tried:



sed -i -e "/localhost/r file2" -e "/localhost/d" file1
sed '/localhost/r file2' file1 |sed '/localhost/d'
sed -e '/localhost/r file2' -e "s///" file1


But I either get the whole line replaced, or the IP going to the line after the one I need to modify.










share|improve this question




















  • 1




    not sure, but does cat file1 | sed -e 's/localhost/1.1.1.1/g' work?
    – dchirikov
    Jul 8 '14 at 18:57






  • 1




    Look at the r sed command.
    – Kevin
    Jul 19 '14 at 15:01















up vote
18
down vote

favorite
1












I have two files: file1 and file2.



file1 has the following contents:



---
host: "localhost"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "localhost:2181"


file2 contains an IP address (1.1.1.1)



What I want to do is replace localhost with 1.1.1.1, so that the end result is:



---
host: "1.1.1.1"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "1.1.1.1:2181"


I have tried:



sed -i -e "/localhost/r file2" -e "/localhost/d" file1
sed '/localhost/r file2' file1 |sed '/localhost/d'
sed -e '/localhost/r file2' -e "s///" file1


But I either get the whole line replaced, or the IP going to the line after the one I need to modify.










share|improve this question




















  • 1




    not sure, but does cat file1 | sed -e 's/localhost/1.1.1.1/g' work?
    – dchirikov
    Jul 8 '14 at 18:57






  • 1




    Look at the r sed command.
    – Kevin
    Jul 19 '14 at 15:01













up vote
18
down vote

favorite
1









up vote
18
down vote

favorite
1






1





I have two files: file1 and file2.



file1 has the following contents:



---
host: "localhost"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "localhost:2181"


file2 contains an IP address (1.1.1.1)



What I want to do is replace localhost with 1.1.1.1, so that the end result is:



---
host: "1.1.1.1"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "1.1.1.1:2181"


I have tried:



sed -i -e "/localhost/r file2" -e "/localhost/d" file1
sed '/localhost/r file2' file1 |sed '/localhost/d'
sed -e '/localhost/r file2' -e "s///" file1


But I either get the whole line replaced, or the IP going to the line after the one I need to modify.










share|improve this question















I have two files: file1 and file2.



file1 has the following contents:



---
host: "localhost"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "localhost:2181"


file2 contains an IP address (1.1.1.1)



What I want to do is replace localhost with 1.1.1.1, so that the end result is:



---
host: "1.1.1.1"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "1.1.1.1:2181"


I have tried:



sed -i -e "/localhost/r file2" -e "/localhost/d" file1
sed '/localhost/r file2' file1 |sed '/localhost/d'
sed -e '/localhost/r file2' -e "s///" file1


But I either get the whole line replaced, or the IP going to the line after the one I need to modify.







text-processing sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 20 '16 at 14:52









don_crissti

49k15129157




49k15129157










asked Jul 8 '14 at 18:23









Jay Kah

91113




91113








  • 1




    not sure, but does cat file1 | sed -e 's/localhost/1.1.1.1/g' work?
    – dchirikov
    Jul 8 '14 at 18:57






  • 1




    Look at the r sed command.
    – Kevin
    Jul 19 '14 at 15:01














  • 1




    not sure, but does cat file1 | sed -e 's/localhost/1.1.1.1/g' work?
    – dchirikov
    Jul 8 '14 at 18:57






  • 1




    Look at the r sed command.
    – Kevin
    Jul 19 '14 at 15:01








1




1




not sure, but does cat file1 | sed -e 's/localhost/1.1.1.1/g' work?
– dchirikov
Jul 8 '14 at 18:57




not sure, but does cat file1 | sed -e 's/localhost/1.1.1.1/g' work?
– dchirikov
Jul 8 '14 at 18:57




1




1




Look at the r sed command.
– Kevin
Jul 19 '14 at 15:01




Look at the r sed command.
– Kevin
Jul 19 '14 at 15:01










4 Answers
4






active

oldest

votes

















up vote
13
down vote













Here is a sed solution:



% sed -e "s/localhost/$(sed 's:/:\/:g' file2)/" file1
---
host: "1.1.1.1"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "1.1.1.1:2181"


You should use sed -i to make the change inplace.



If you can use awk, here is one way to do:



% awk 'BEGIN{getline l < "file2"}/localhost/{gsub("localhost",l)}1' file1
---
host: "1.1.1.1"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "1.1.1.1:2181"





share|improve this answer



















  • 4




    +1 for awk. I imagine sed is capable of this, but it will be terribly clumsy. This is where awk shines!
    – HalosGhost
    Jul 8 '14 at 19:14






  • 1




    @HalosGhost: It's seem both me and you had misunderstand the OP question, I updated my answer.
    – cuonglm
    Jul 8 '14 at 19:25










  • The command substitution for the sed solution should be double quoted in case the file contains spaces or glob characters.
    – Graeme
    Jul 8 '14 at 19:38










  • @Graeme: Thanks, updated! Feel free to make an editting.
    – cuonglm
    Jul 8 '14 at 19:39






  • 2




    You need to escape both / and & in the substitution. That's "$(sed 's:[/\&]:\&:g' file2)"
    – Toby Speight
    Dec 6 '16 at 12:40




















up vote
6
down vote













You can read the file with the replacement string using shell command substitution, before sed is used. So sed will see just a normal substitution:



sed "s/localhost/$(cat file2)/" file1 > changed.txt






share|improve this answer

















  • 13




    Does this work on multi-lined files and files with special characters?
    – Trevor Hickey
    Jun 12 '15 at 18:23






  • 4




    @TrevorHickey it doesn't. Sed just fails with an "unterminated `s' command" error.
    – DarioP
    Oct 7 '16 at 11:16


















up vote
1
down vote













Try using



join file1 file2


and then, remove any unwanted fields.






share|improve this answer




























    up vote
    0
    down vote













    I also had this "problem" today: how to replace a block of text with the contents from another file.



    I've solved it by making a bash function (that can be reused in scripts). Hope it helps others



    [cent@pcmk-1 tmp]$ cat the_function.sh
    # This function reads text from stdin, and substitutes a *BLOCK* with the contents from a FILE, and outputs to stdout
    # The BLOCK is indicated with BLOCK_StartRegexp and BLOCK_EndRegexp
    #
    # Usage:
    # seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
    function substitute_BLOCK_with_FILEcontents {
    local BLOCK_StartRegexp="${1}"
    local BLOCK_EndRegexp="${2}"
    local FILE="${3}"
    sed -e "/${BLOCK_EndRegexp}/a ___tmpMark___" -e "/${BLOCK_StartRegexp}/,/${BLOCK_EndRegexp}/d" | sed -e "/___tmpMark___/r ${FILE}" -e '/___tmpMark___/d'
    }

    [cent@pcmk-1 tmp]$
    [cent@pcmk-1 tmp]$
    [cent@pcmk-1 tmp]$ cat /tmp/FileWithContents
    We have deleted everyhing between lines 102 and 104 and
    replaced with this text, which was read from a file
    [cent@pcmk-1 tmp]$
    [cent@pcmk-1 tmp]$
    [cent@pcmk-1 tmp]$ source the_function.sh
    [cent@pcmk-1 tmp]$ seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
    [cent@pcmk-1 tmp]$
    [cent@pcmk-1 tmp]$
    [cent@pcmk-1 tmp]$ cat /tmp/result.txt
    100
    101
    We have deleted everyhing between lines 102 and 104 and
    replaced with this text, which was read from a file
    105
    106
    107
    108
    109
    110





    share|improve this answer








    New contributor




    zipizap 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
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f141387%2fsed-replace-string-with-file-contents%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      13
      down vote













      Here is a sed solution:



      % sed -e "s/localhost/$(sed 's:/:\/:g' file2)/" file1
      ---
      host: "1.1.1.1"
      port: 3000
      reporter_type: "zookeeper"
      zk_hosts:
      - "1.1.1.1:2181"


      You should use sed -i to make the change inplace.



      If you can use awk, here is one way to do:



      % awk 'BEGIN{getline l < "file2"}/localhost/{gsub("localhost",l)}1' file1
      ---
      host: "1.1.1.1"
      port: 3000
      reporter_type: "zookeeper"
      zk_hosts:
      - "1.1.1.1:2181"





      share|improve this answer



















      • 4




        +1 for awk. I imagine sed is capable of this, but it will be terribly clumsy. This is where awk shines!
        – HalosGhost
        Jul 8 '14 at 19:14






      • 1




        @HalosGhost: It's seem both me and you had misunderstand the OP question, I updated my answer.
        – cuonglm
        Jul 8 '14 at 19:25










      • The command substitution for the sed solution should be double quoted in case the file contains spaces or glob characters.
        – Graeme
        Jul 8 '14 at 19:38










      • @Graeme: Thanks, updated! Feel free to make an editting.
        – cuonglm
        Jul 8 '14 at 19:39






      • 2




        You need to escape both / and & in the substitution. That's "$(sed 's:[/\&]:\&:g' file2)"
        – Toby Speight
        Dec 6 '16 at 12:40

















      up vote
      13
      down vote













      Here is a sed solution:



      % sed -e "s/localhost/$(sed 's:/:\/:g' file2)/" file1
      ---
      host: "1.1.1.1"
      port: 3000
      reporter_type: "zookeeper"
      zk_hosts:
      - "1.1.1.1:2181"


      You should use sed -i to make the change inplace.



      If you can use awk, here is one way to do:



      % awk 'BEGIN{getline l < "file2"}/localhost/{gsub("localhost",l)}1' file1
      ---
      host: "1.1.1.1"
      port: 3000
      reporter_type: "zookeeper"
      zk_hosts:
      - "1.1.1.1:2181"





      share|improve this answer



















      • 4




        +1 for awk. I imagine sed is capable of this, but it will be terribly clumsy. This is where awk shines!
        – HalosGhost
        Jul 8 '14 at 19:14






      • 1




        @HalosGhost: It's seem both me and you had misunderstand the OP question, I updated my answer.
        – cuonglm
        Jul 8 '14 at 19:25










      • The command substitution for the sed solution should be double quoted in case the file contains spaces or glob characters.
        – Graeme
        Jul 8 '14 at 19:38










      • @Graeme: Thanks, updated! Feel free to make an editting.
        – cuonglm
        Jul 8 '14 at 19:39






      • 2




        You need to escape both / and & in the substitution. That's "$(sed 's:[/\&]:\&:g' file2)"
        – Toby Speight
        Dec 6 '16 at 12:40















      up vote
      13
      down vote










      up vote
      13
      down vote









      Here is a sed solution:



      % sed -e "s/localhost/$(sed 's:/:\/:g' file2)/" file1
      ---
      host: "1.1.1.1"
      port: 3000
      reporter_type: "zookeeper"
      zk_hosts:
      - "1.1.1.1:2181"


      You should use sed -i to make the change inplace.



      If you can use awk, here is one way to do:



      % awk 'BEGIN{getline l < "file2"}/localhost/{gsub("localhost",l)}1' file1
      ---
      host: "1.1.1.1"
      port: 3000
      reporter_type: "zookeeper"
      zk_hosts:
      - "1.1.1.1:2181"





      share|improve this answer














      Here is a sed solution:



      % sed -e "s/localhost/$(sed 's:/:\/:g' file2)/" file1
      ---
      host: "1.1.1.1"
      port: 3000
      reporter_type: "zookeeper"
      zk_hosts:
      - "1.1.1.1:2181"


      You should use sed -i to make the change inplace.



      If you can use awk, here is one way to do:



      % awk 'BEGIN{getline l < "file2"}/localhost/{gsub("localhost",l)}1' file1
      ---
      host: "1.1.1.1"
      port: 3000
      reporter_type: "zookeeper"
      zk_hosts:
      - "1.1.1.1:2181"






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jul 8 '14 at 19:44









      Graeme

      24.8k46296




      24.8k46296










      answered Jul 8 '14 at 19:13









      cuonglm

      101k23197299




      101k23197299








      • 4




        +1 for awk. I imagine sed is capable of this, but it will be terribly clumsy. This is where awk shines!
        – HalosGhost
        Jul 8 '14 at 19:14






      • 1




        @HalosGhost: It's seem both me and you had misunderstand the OP question, I updated my answer.
        – cuonglm
        Jul 8 '14 at 19:25










      • The command substitution for the sed solution should be double quoted in case the file contains spaces or glob characters.
        – Graeme
        Jul 8 '14 at 19:38










      • @Graeme: Thanks, updated! Feel free to make an editting.
        – cuonglm
        Jul 8 '14 at 19:39






      • 2




        You need to escape both / and & in the substitution. That's "$(sed 's:[/\&]:\&:g' file2)"
        – Toby Speight
        Dec 6 '16 at 12:40
















      • 4




        +1 for awk. I imagine sed is capable of this, but it will be terribly clumsy. This is where awk shines!
        – HalosGhost
        Jul 8 '14 at 19:14






      • 1




        @HalosGhost: It's seem both me and you had misunderstand the OP question, I updated my answer.
        – cuonglm
        Jul 8 '14 at 19:25










      • The command substitution for the sed solution should be double quoted in case the file contains spaces or glob characters.
        – Graeme
        Jul 8 '14 at 19:38










      • @Graeme: Thanks, updated! Feel free to make an editting.
        – cuonglm
        Jul 8 '14 at 19:39






      • 2




        You need to escape both / and & in the substitution. That's "$(sed 's:[/\&]:\&:g' file2)"
        – Toby Speight
        Dec 6 '16 at 12:40










      4




      4




      +1 for awk. I imagine sed is capable of this, but it will be terribly clumsy. This is where awk shines!
      – HalosGhost
      Jul 8 '14 at 19:14




      +1 for awk. I imagine sed is capable of this, but it will be terribly clumsy. This is where awk shines!
      – HalosGhost
      Jul 8 '14 at 19:14




      1




      1




      @HalosGhost: It's seem both me and you had misunderstand the OP question, I updated my answer.
      – cuonglm
      Jul 8 '14 at 19:25




      @HalosGhost: It's seem both me and you had misunderstand the OP question, I updated my answer.
      – cuonglm
      Jul 8 '14 at 19:25












      The command substitution for the sed solution should be double quoted in case the file contains spaces or glob characters.
      – Graeme
      Jul 8 '14 at 19:38




      The command substitution for the sed solution should be double quoted in case the file contains spaces or glob characters.
      – Graeme
      Jul 8 '14 at 19:38












      @Graeme: Thanks, updated! Feel free to make an editting.
      – cuonglm
      Jul 8 '14 at 19:39




      @Graeme: Thanks, updated! Feel free to make an editting.
      – cuonglm
      Jul 8 '14 at 19:39




      2




      2




      You need to escape both / and & in the substitution. That's "$(sed 's:[/\&]:\&:g' file2)"
      – Toby Speight
      Dec 6 '16 at 12:40






      You need to escape both / and & in the substitution. That's "$(sed 's:[/\&]:\&:g' file2)"
      – Toby Speight
      Dec 6 '16 at 12:40














      up vote
      6
      down vote













      You can read the file with the replacement string using shell command substitution, before sed is used. So sed will see just a normal substitution:



      sed "s/localhost/$(cat file2)/" file1 > changed.txt






      share|improve this answer

















      • 13




        Does this work on multi-lined files and files with special characters?
        – Trevor Hickey
        Jun 12 '15 at 18:23






      • 4




        @TrevorHickey it doesn't. Sed just fails with an "unterminated `s' command" error.
        – DarioP
        Oct 7 '16 at 11:16















      up vote
      6
      down vote













      You can read the file with the replacement string using shell command substitution, before sed is used. So sed will see just a normal substitution:



      sed "s/localhost/$(cat file2)/" file1 > changed.txt






      share|improve this answer

















      • 13




        Does this work on multi-lined files and files with special characters?
        – Trevor Hickey
        Jun 12 '15 at 18:23






      • 4




        @TrevorHickey it doesn't. Sed just fails with an "unterminated `s' command" error.
        – DarioP
        Oct 7 '16 at 11:16













      up vote
      6
      down vote










      up vote
      6
      down vote









      You can read the file with the replacement string using shell command substitution, before sed is used. So sed will see just a normal substitution:



      sed "s/localhost/$(cat file2)/" file1 > changed.txt






      share|improve this answer












      You can read the file with the replacement string using shell command substitution, before sed is used. So sed will see just a normal substitution:



      sed "s/localhost/$(cat file2)/" file1 > changed.txt







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jul 8 '14 at 19:12









      Volker Siegel

      10.7k33258




      10.7k33258








      • 13




        Does this work on multi-lined files and files with special characters?
        – Trevor Hickey
        Jun 12 '15 at 18:23






      • 4




        @TrevorHickey it doesn't. Sed just fails with an "unterminated `s' command" error.
        – DarioP
        Oct 7 '16 at 11:16














      • 13




        Does this work on multi-lined files and files with special characters?
        – Trevor Hickey
        Jun 12 '15 at 18:23






      • 4




        @TrevorHickey it doesn't. Sed just fails with an "unterminated `s' command" error.
        – DarioP
        Oct 7 '16 at 11:16








      13




      13




      Does this work on multi-lined files and files with special characters?
      – Trevor Hickey
      Jun 12 '15 at 18:23




      Does this work on multi-lined files and files with special characters?
      – Trevor Hickey
      Jun 12 '15 at 18:23




      4




      4




      @TrevorHickey it doesn't. Sed just fails with an "unterminated `s' command" error.
      – DarioP
      Oct 7 '16 at 11:16




      @TrevorHickey it doesn't. Sed just fails with an "unterminated `s' command" error.
      – DarioP
      Oct 7 '16 at 11:16










      up vote
      1
      down vote













      Try using



      join file1 file2


      and then, remove any unwanted fields.






      share|improve this answer

























        up vote
        1
        down vote













        Try using



        join file1 file2


        and then, remove any unwanted fields.






        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          Try using



          join file1 file2


          and then, remove any unwanted fields.






          share|improve this answer












          Try using



          join file1 file2


          and then, remove any unwanted fields.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jul 8 '14 at 18:46









          unxnut

          3,5622918




          3,5622918






















              up vote
              0
              down vote













              I also had this "problem" today: how to replace a block of text with the contents from another file.



              I've solved it by making a bash function (that can be reused in scripts). Hope it helps others



              [cent@pcmk-1 tmp]$ cat the_function.sh
              # This function reads text from stdin, and substitutes a *BLOCK* with the contents from a FILE, and outputs to stdout
              # The BLOCK is indicated with BLOCK_StartRegexp and BLOCK_EndRegexp
              #
              # Usage:
              # seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
              function substitute_BLOCK_with_FILEcontents {
              local BLOCK_StartRegexp="${1}"
              local BLOCK_EndRegexp="${2}"
              local FILE="${3}"
              sed -e "/${BLOCK_EndRegexp}/a ___tmpMark___" -e "/${BLOCK_StartRegexp}/,/${BLOCK_EndRegexp}/d" | sed -e "/___tmpMark___/r ${FILE}" -e '/___tmpMark___/d'
              }

              [cent@pcmk-1 tmp]$
              [cent@pcmk-1 tmp]$
              [cent@pcmk-1 tmp]$ cat /tmp/FileWithContents
              We have deleted everyhing between lines 102 and 104 and
              replaced with this text, which was read from a file
              [cent@pcmk-1 tmp]$
              [cent@pcmk-1 tmp]$
              [cent@pcmk-1 tmp]$ source the_function.sh
              [cent@pcmk-1 tmp]$ seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
              [cent@pcmk-1 tmp]$
              [cent@pcmk-1 tmp]$
              [cent@pcmk-1 tmp]$ cat /tmp/result.txt
              100
              101
              We have deleted everyhing between lines 102 and 104 and
              replaced with this text, which was read from a file
              105
              106
              107
              108
              109
              110





              share|improve this answer








              New contributor




              zipizap 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













                I also had this "problem" today: how to replace a block of text with the contents from another file.



                I've solved it by making a bash function (that can be reused in scripts). Hope it helps others



                [cent@pcmk-1 tmp]$ cat the_function.sh
                # This function reads text from stdin, and substitutes a *BLOCK* with the contents from a FILE, and outputs to stdout
                # The BLOCK is indicated with BLOCK_StartRegexp and BLOCK_EndRegexp
                #
                # Usage:
                # seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
                function substitute_BLOCK_with_FILEcontents {
                local BLOCK_StartRegexp="${1}"
                local BLOCK_EndRegexp="${2}"
                local FILE="${3}"
                sed -e "/${BLOCK_EndRegexp}/a ___tmpMark___" -e "/${BLOCK_StartRegexp}/,/${BLOCK_EndRegexp}/d" | sed -e "/___tmpMark___/r ${FILE}" -e '/___tmpMark___/d'
                }

                [cent@pcmk-1 tmp]$
                [cent@pcmk-1 tmp]$
                [cent@pcmk-1 tmp]$ cat /tmp/FileWithContents
                We have deleted everyhing between lines 102 and 104 and
                replaced with this text, which was read from a file
                [cent@pcmk-1 tmp]$
                [cent@pcmk-1 tmp]$
                [cent@pcmk-1 tmp]$ source the_function.sh
                [cent@pcmk-1 tmp]$ seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
                [cent@pcmk-1 tmp]$
                [cent@pcmk-1 tmp]$
                [cent@pcmk-1 tmp]$ cat /tmp/result.txt
                100
                101
                We have deleted everyhing between lines 102 and 104 and
                replaced with this text, which was read from a file
                105
                106
                107
                108
                109
                110





                share|improve this answer








                New contributor




                zipizap 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










                  up vote
                  0
                  down vote









                  I also had this "problem" today: how to replace a block of text with the contents from another file.



                  I've solved it by making a bash function (that can be reused in scripts). Hope it helps others



                  [cent@pcmk-1 tmp]$ cat the_function.sh
                  # This function reads text from stdin, and substitutes a *BLOCK* with the contents from a FILE, and outputs to stdout
                  # The BLOCK is indicated with BLOCK_StartRegexp and BLOCK_EndRegexp
                  #
                  # Usage:
                  # seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
                  function substitute_BLOCK_with_FILEcontents {
                  local BLOCK_StartRegexp="${1}"
                  local BLOCK_EndRegexp="${2}"
                  local FILE="${3}"
                  sed -e "/${BLOCK_EndRegexp}/a ___tmpMark___" -e "/${BLOCK_StartRegexp}/,/${BLOCK_EndRegexp}/d" | sed -e "/___tmpMark___/r ${FILE}" -e '/___tmpMark___/d'
                  }

                  [cent@pcmk-1 tmp]$
                  [cent@pcmk-1 tmp]$
                  [cent@pcmk-1 tmp]$ cat /tmp/FileWithContents
                  We have deleted everyhing between lines 102 and 104 and
                  replaced with this text, which was read from a file
                  [cent@pcmk-1 tmp]$
                  [cent@pcmk-1 tmp]$
                  [cent@pcmk-1 tmp]$ source the_function.sh
                  [cent@pcmk-1 tmp]$ seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
                  [cent@pcmk-1 tmp]$
                  [cent@pcmk-1 tmp]$
                  [cent@pcmk-1 tmp]$ cat /tmp/result.txt
                  100
                  101
                  We have deleted everyhing between lines 102 and 104 and
                  replaced with this text, which was read from a file
                  105
                  106
                  107
                  108
                  109
                  110





                  share|improve this answer








                  New contributor




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









                  I also had this "problem" today: how to replace a block of text with the contents from another file.



                  I've solved it by making a bash function (that can be reused in scripts). Hope it helps others



                  [cent@pcmk-1 tmp]$ cat the_function.sh
                  # This function reads text from stdin, and substitutes a *BLOCK* with the contents from a FILE, and outputs to stdout
                  # The BLOCK is indicated with BLOCK_StartRegexp and BLOCK_EndRegexp
                  #
                  # Usage:
                  # seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
                  function substitute_BLOCK_with_FILEcontents {
                  local BLOCK_StartRegexp="${1}"
                  local BLOCK_EndRegexp="${2}"
                  local FILE="${3}"
                  sed -e "/${BLOCK_EndRegexp}/a ___tmpMark___" -e "/${BLOCK_StartRegexp}/,/${BLOCK_EndRegexp}/d" | sed -e "/___tmpMark___/r ${FILE}" -e '/___tmpMark___/d'
                  }

                  [cent@pcmk-1 tmp]$
                  [cent@pcmk-1 tmp]$
                  [cent@pcmk-1 tmp]$ cat /tmp/FileWithContents
                  We have deleted everyhing between lines 102 and 104 and
                  replaced with this text, which was read from a file
                  [cent@pcmk-1 tmp]$
                  [cent@pcmk-1 tmp]$
                  [cent@pcmk-1 tmp]$ source the_function.sh
                  [cent@pcmk-1 tmp]$ seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
                  [cent@pcmk-1 tmp]$
                  [cent@pcmk-1 tmp]$
                  [cent@pcmk-1 tmp]$ cat /tmp/result.txt
                  100
                  101
                  We have deleted everyhing between lines 102 and 104 and
                  replaced with this text, which was read from a file
                  105
                  106
                  107
                  108
                  109
                  110






                  share|improve this answer








                  New contributor




                  zipizap 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




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









                  answered Dec 3 at 12:09









                  zipizap

                  101




                  101




                  New contributor




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





                  New contributor





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






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






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f141387%2fsed-replace-string-with-file-contents%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

                      サソリ

                      広島県道265号伴広島線

                      Accessing regular linux commands in Huawei's Dopra Linux