Replace fenced block with output of command in bash

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











up vote
1
down vote

favorite












I'm attempting to replace a "fenced" block of text with the output from the tree command in linux (specifically in a make file). Here's my input file:



some text...
[fencetitle]
----
.
├── file1.txt
└── test
└── file2.txt

1 directory, 2 files
----
some more text...


And I'd like to replace the contents with output from the tree command:



some text...
[fencetitle]
----
.
├── file1.txt
├── newfile.txt
└── test
└── file2.txt

1 directory, 3 files
----
some more text...


Using the sed command, I can't seem to match using newlines with the line [fencedtitle] and ---- to ----. I am able to replace text between [fencedtitle] and ---- with the following command, however:



sed -n '/[fencedtitle]/:a;N;/----/!ba;N;s/.*n/REPLACEMENTn/;p' file


But I can't seem to then replace REPLACEMENT with the output from the tree command. Is sed the right approach here, or is something else more appropriate?







share|improve this question


























    up vote
    1
    down vote

    favorite












    I'm attempting to replace a "fenced" block of text with the output from the tree command in linux (specifically in a make file). Here's my input file:



    some text...
    [fencetitle]
    ----
    .
    ├── file1.txt
    └── test
    └── file2.txt

    1 directory, 2 files
    ----
    some more text...


    And I'd like to replace the contents with output from the tree command:



    some text...
    [fencetitle]
    ----
    .
    ├── file1.txt
    ├── newfile.txt
    └── test
    └── file2.txt

    1 directory, 3 files
    ----
    some more text...


    Using the sed command, I can't seem to match using newlines with the line [fencedtitle] and ---- to ----. I am able to replace text between [fencedtitle] and ---- with the following command, however:



    sed -n '/[fencedtitle]/:a;N;/----/!ba;N;s/.*n/REPLACEMENTn/;p' file


    But I can't seem to then replace REPLACEMENT with the output from the tree command. Is sed the right approach here, or is something else more appropriate?







    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm attempting to replace a "fenced" block of text with the output from the tree command in linux (specifically in a make file). Here's my input file:



      some text...
      [fencetitle]
      ----
      .
      ├── file1.txt
      └── test
      └── file2.txt

      1 directory, 2 files
      ----
      some more text...


      And I'd like to replace the contents with output from the tree command:



      some text...
      [fencetitle]
      ----
      .
      ├── file1.txt
      ├── newfile.txt
      └── test
      └── file2.txt

      1 directory, 3 files
      ----
      some more text...


      Using the sed command, I can't seem to match using newlines with the line [fencedtitle] and ---- to ----. I am able to replace text between [fencedtitle] and ---- with the following command, however:



      sed -n '/[fencedtitle]/:a;N;/----/!ba;N;s/.*n/REPLACEMENTn/;p' file


      But I can't seem to then replace REPLACEMENT with the output from the tree command. Is sed the right approach here, or is something else more appropriate?







      share|improve this question














      I'm attempting to replace a "fenced" block of text with the output from the tree command in linux (specifically in a make file). Here's my input file:



      some text...
      [fencetitle]
      ----
      .
      ├── file1.txt
      └── test
      └── file2.txt

      1 directory, 2 files
      ----
      some more text...


      And I'd like to replace the contents with output from the tree command:



      some text...
      [fencetitle]
      ----
      .
      ├── file1.txt
      ├── newfile.txt
      └── test
      └── file2.txt

      1 directory, 3 files
      ----
      some more text...


      Using the sed command, I can't seem to match using newlines with the line [fencedtitle] and ---- to ----. I am able to replace text between [fencedtitle] and ---- with the following command, however:



      sed -n '/[fencedtitle]/:a;N;/----/!ba;N;s/.*n/REPLACEMENTn/;p' file


      But I can't seem to then replace REPLACEMENT with the output from the tree command. Is sed the right approach here, or is something else more appropriate?









      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 19 at 1:31









      Jeff Schaller

      31.2k846105




      31.2k846105










      asked Feb 19 at 1:10









      John Ericksen

      1084




      1084




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          You could try something like:



          sed -n "/[fencedtitle]/:a;N;/----/!ba;N;s/.*n/$(tree)n/;p" file


          But this will be problematic if the output of tree contains characters that are special for regex. If you save the output of tree to a file:



          tree > my-out


          Then you can read it in sed without too many problems:



          sed -n '/[fencetitle]/p;n;p;r my-out
          :a;n;/^----$/!ba;p' file


          (Yes, the r command does require a new line after it, no commands may follow it on the same line.)



          The p;n;p; just prints the start of the fence, then we read the file, then we skip everything till the end of the fence.



          You can end the line after the r command by splitting out the rest into a separate sed expression:



          sed -ne '/[fencetitle]/p;n;p;r my-out' -e ':a;n;/^----$/!ba;p' file





          share|improve this answer






















          • Great, that seems to do the trick... any idea how I could include that newline in a makefile?
            – John Ericksen
            Feb 19 at 3:57










          • @JohnEricksen if you split it into two -e expressions, you can avoid the newline, see updated answer.
            – Olorin
            Feb 19 at 4:03










          • Hmm, I'm getting a bash: !ba}: event not found from the latest command.
            – John Ericksen
            Feb 19 at 4:08










          • Did you use double quotes instead of single quotes?
            – Olorin
            Feb 19 at 4:11






          • 2




            @JohnEricksen Remember you need to escape $ in a makefile by using $$. You can also add -i to the sed to actually change file.
            – meuh
            Feb 19 at 8:00

















          up vote
          0
          down vote













          I have done by below method for above example



          sed "s/file1.txt/&n|__newfile/g" example.txt


          output



          some text...
          [fencetitle]
          ----
          .
          ├── file1.txt
          |__newfile
          └── test
          └── file2.txt





          share|improve this answer




















          • I think you took the example too literally; they’re looking for the output of the tree command to be put between the bracketed text, not a hard-coded replacement after file1.txt of “newfile”.
            – Jeff Schaller
            Feb 20 at 3:36










          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%2f425054%2freplace-fenced-block-with-output-of-command-in-bash%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
          2
          down vote



          accepted










          You could try something like:



          sed -n "/[fencedtitle]/:a;N;/----/!ba;N;s/.*n/$(tree)n/;p" file


          But this will be problematic if the output of tree contains characters that are special for regex. If you save the output of tree to a file:



          tree > my-out


          Then you can read it in sed without too many problems:



          sed -n '/[fencetitle]/p;n;p;r my-out
          :a;n;/^----$/!ba;p' file


          (Yes, the r command does require a new line after it, no commands may follow it on the same line.)



          The p;n;p; just prints the start of the fence, then we read the file, then we skip everything till the end of the fence.



          You can end the line after the r command by splitting out the rest into a separate sed expression:



          sed -ne '/[fencetitle]/p;n;p;r my-out' -e ':a;n;/^----$/!ba;p' file





          share|improve this answer






















          • Great, that seems to do the trick... any idea how I could include that newline in a makefile?
            – John Ericksen
            Feb 19 at 3:57










          • @JohnEricksen if you split it into two -e expressions, you can avoid the newline, see updated answer.
            – Olorin
            Feb 19 at 4:03










          • Hmm, I'm getting a bash: !ba}: event not found from the latest command.
            – John Ericksen
            Feb 19 at 4:08










          • Did you use double quotes instead of single quotes?
            – Olorin
            Feb 19 at 4:11






          • 2




            @JohnEricksen Remember you need to escape $ in a makefile by using $$. You can also add -i to the sed to actually change file.
            – meuh
            Feb 19 at 8:00














          up vote
          2
          down vote



          accepted










          You could try something like:



          sed -n "/[fencedtitle]/:a;N;/----/!ba;N;s/.*n/$(tree)n/;p" file


          But this will be problematic if the output of tree contains characters that are special for regex. If you save the output of tree to a file:



          tree > my-out


          Then you can read it in sed without too many problems:



          sed -n '/[fencetitle]/p;n;p;r my-out
          :a;n;/^----$/!ba;p' file


          (Yes, the r command does require a new line after it, no commands may follow it on the same line.)



          The p;n;p; just prints the start of the fence, then we read the file, then we skip everything till the end of the fence.



          You can end the line after the r command by splitting out the rest into a separate sed expression:



          sed -ne '/[fencetitle]/p;n;p;r my-out' -e ':a;n;/^----$/!ba;p' file





          share|improve this answer






















          • Great, that seems to do the trick... any idea how I could include that newline in a makefile?
            – John Ericksen
            Feb 19 at 3:57










          • @JohnEricksen if you split it into two -e expressions, you can avoid the newline, see updated answer.
            – Olorin
            Feb 19 at 4:03










          • Hmm, I'm getting a bash: !ba}: event not found from the latest command.
            – John Ericksen
            Feb 19 at 4:08










          • Did you use double quotes instead of single quotes?
            – Olorin
            Feb 19 at 4:11






          • 2




            @JohnEricksen Remember you need to escape $ in a makefile by using $$. You can also add -i to the sed to actually change file.
            – meuh
            Feb 19 at 8:00












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          You could try something like:



          sed -n "/[fencedtitle]/:a;N;/----/!ba;N;s/.*n/$(tree)n/;p" file


          But this will be problematic if the output of tree contains characters that are special for regex. If you save the output of tree to a file:



          tree > my-out


          Then you can read it in sed without too many problems:



          sed -n '/[fencetitle]/p;n;p;r my-out
          :a;n;/^----$/!ba;p' file


          (Yes, the r command does require a new line after it, no commands may follow it on the same line.)



          The p;n;p; just prints the start of the fence, then we read the file, then we skip everything till the end of the fence.



          You can end the line after the r command by splitting out the rest into a separate sed expression:



          sed -ne '/[fencetitle]/p;n;p;r my-out' -e ':a;n;/^----$/!ba;p' file





          share|improve this answer














          You could try something like:



          sed -n "/[fencedtitle]/:a;N;/----/!ba;N;s/.*n/$(tree)n/;p" file


          But this will be problematic if the output of tree contains characters that are special for regex. If you save the output of tree to a file:



          tree > my-out


          Then you can read it in sed without too many problems:



          sed -n '/[fencetitle]/p;n;p;r my-out
          :a;n;/^----$/!ba;p' file


          (Yes, the r command does require a new line after it, no commands may follow it on the same line.)



          The p;n;p; just prints the start of the fence, then we read the file, then we skip everything till the end of the fence.



          You can end the line after the r command by splitting out the rest into a separate sed expression:



          sed -ne '/[fencetitle]/p;n;p;r my-out' -e ':a;n;/^----$/!ba;p' file






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 19 at 4:02

























          answered Feb 19 at 2:33









          Olorin

          1,15711




          1,15711











          • Great, that seems to do the trick... any idea how I could include that newline in a makefile?
            – John Ericksen
            Feb 19 at 3:57










          • @JohnEricksen if you split it into two -e expressions, you can avoid the newline, see updated answer.
            – Olorin
            Feb 19 at 4:03










          • Hmm, I'm getting a bash: !ba}: event not found from the latest command.
            – John Ericksen
            Feb 19 at 4:08










          • Did you use double quotes instead of single quotes?
            – Olorin
            Feb 19 at 4:11






          • 2




            @JohnEricksen Remember you need to escape $ in a makefile by using $$. You can also add -i to the sed to actually change file.
            – meuh
            Feb 19 at 8:00
















          • Great, that seems to do the trick... any idea how I could include that newline in a makefile?
            – John Ericksen
            Feb 19 at 3:57










          • @JohnEricksen if you split it into two -e expressions, you can avoid the newline, see updated answer.
            – Olorin
            Feb 19 at 4:03










          • Hmm, I'm getting a bash: !ba}: event not found from the latest command.
            – John Ericksen
            Feb 19 at 4:08










          • Did you use double quotes instead of single quotes?
            – Olorin
            Feb 19 at 4:11






          • 2




            @JohnEricksen Remember you need to escape $ in a makefile by using $$. You can also add -i to the sed to actually change file.
            – meuh
            Feb 19 at 8:00















          Great, that seems to do the trick... any idea how I could include that newline in a makefile?
          – John Ericksen
          Feb 19 at 3:57




          Great, that seems to do the trick... any idea how I could include that newline in a makefile?
          – John Ericksen
          Feb 19 at 3:57












          @JohnEricksen if you split it into two -e expressions, you can avoid the newline, see updated answer.
          – Olorin
          Feb 19 at 4:03




          @JohnEricksen if you split it into two -e expressions, you can avoid the newline, see updated answer.
          – Olorin
          Feb 19 at 4:03












          Hmm, I'm getting a bash: !ba}: event not found from the latest command.
          – John Ericksen
          Feb 19 at 4:08




          Hmm, I'm getting a bash: !ba}: event not found from the latest command.
          – John Ericksen
          Feb 19 at 4:08












          Did you use double quotes instead of single quotes?
          – Olorin
          Feb 19 at 4:11




          Did you use double quotes instead of single quotes?
          – Olorin
          Feb 19 at 4:11




          2




          2




          @JohnEricksen Remember you need to escape $ in a makefile by using $$. You can also add -i to the sed to actually change file.
          – meuh
          Feb 19 at 8:00




          @JohnEricksen Remember you need to escape $ in a makefile by using $$. You can also add -i to the sed to actually change file.
          – meuh
          Feb 19 at 8:00












          up vote
          0
          down vote













          I have done by below method for above example



          sed "s/file1.txt/&n|__newfile/g" example.txt


          output



          some text...
          [fencetitle]
          ----
          .
          ├── file1.txt
          |__newfile
          └── test
          └── file2.txt





          share|improve this answer




















          • I think you took the example too literally; they’re looking for the output of the tree command to be put between the bracketed text, not a hard-coded replacement after file1.txt of “newfile”.
            – Jeff Schaller
            Feb 20 at 3:36














          up vote
          0
          down vote













          I have done by below method for above example



          sed "s/file1.txt/&n|__newfile/g" example.txt


          output



          some text...
          [fencetitle]
          ----
          .
          ├── file1.txt
          |__newfile
          └── test
          └── file2.txt





          share|improve this answer




















          • I think you took the example too literally; they’re looking for the output of the tree command to be put between the bracketed text, not a hard-coded replacement after file1.txt of “newfile”.
            – Jeff Schaller
            Feb 20 at 3:36












          up vote
          0
          down vote










          up vote
          0
          down vote









          I have done by below method for above example



          sed "s/file1.txt/&n|__newfile/g" example.txt


          output



          some text...
          [fencetitle]
          ----
          .
          ├── file1.txt
          |__newfile
          └── test
          └── file2.txt





          share|improve this answer












          I have done by below method for above example



          sed "s/file1.txt/&n|__newfile/g" example.txt


          output



          some text...
          [fencetitle]
          ----
          .
          ├── file1.txt
          |__newfile
          └── test
          └── file2.txt






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 19 at 10:33









          Praveen Kumar BS

          1,010128




          1,010128











          • I think you took the example too literally; they’re looking for the output of the tree command to be put between the bracketed text, not a hard-coded replacement after file1.txt of “newfile”.
            – Jeff Schaller
            Feb 20 at 3:36
















          • I think you took the example too literally; they’re looking for the output of the tree command to be put between the bracketed text, not a hard-coded replacement after file1.txt of “newfile”.
            – Jeff Schaller
            Feb 20 at 3:36















          I think you took the example too literally; they’re looking for the output of the tree command to be put between the bracketed text, not a hard-coded replacement after file1.txt of “newfile”.
          – Jeff Schaller
          Feb 20 at 3:36




          I think you took the example too literally; they’re looking for the output of the tree command to be put between the bracketed text, not a hard-coded replacement after file1.txt of “newfile”.
          – Jeff Schaller
          Feb 20 at 3:36












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f425054%2freplace-fenced-block-with-output-of-command-in-bash%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Peggy Mitchell

          Palaiologos

          The Forum (Inglewood, California)