Replace fenced block with output of command in bash

Clash 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?
text-processing sed tree
add a comment |Â
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?
text-processing sed tree
add a comment |Â
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?
text-processing sed tree
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?
text-processing sed tree
edited Feb 19 at 1:31
Jeff Schaller
31.2k846105
31.2k846105
asked Feb 19 at 1:10
John Ericksen
1084
1084
add a comment |Â
add a comment |Â
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
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-eexpressions, you can avoid the newline, see updated answer.
â Olorin
Feb 19 at 4:03
Hmm, I'm getting abash: !ba}: event not foundfrom 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-ito the sed to actually change file.
â meuh
Feb 19 at 8:00
 |Â
show 3 more comments
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
I think you took the example too literally; theyâÂÂre looking for the output of thetreecommand to be put between the bracketed text, not a hard-coded replacement after file1.txt of âÂÂnewfileâÂÂ.
â Jeff Schaller
Feb 20 at 3:36
add a comment |Â
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
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-eexpressions, you can avoid the newline, see updated answer.
â Olorin
Feb 19 at 4:03
Hmm, I'm getting abash: !ba}: event not foundfrom 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-ito the sed to actually change file.
â meuh
Feb 19 at 8:00
 |Â
show 3 more comments
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
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-eexpressions, you can avoid the newline, see updated answer.
â Olorin
Feb 19 at 4:03
Hmm, I'm getting abash: !ba}: event not foundfrom 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-ito the sed to actually change file.
â meuh
Feb 19 at 8:00
 |Â
show 3 more comments
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
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
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-eexpressions, you can avoid the newline, see updated answer.
â Olorin
Feb 19 at 4:03
Hmm, I'm getting abash: !ba}: event not foundfrom 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-ito the sed to actually change file.
â meuh
Feb 19 at 8:00
 |Â
show 3 more comments
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-eexpressions, you can avoid the newline, see updated answer.
â Olorin
Feb 19 at 4:03
Hmm, I'm getting abash: !ba}: event not foundfrom 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-ito 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
 |Â
show 3 more comments
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
I think you took the example too literally; theyâÂÂre looking for the output of thetreecommand to be put between the bracketed text, not a hard-coded replacement after file1.txt of âÂÂnewfileâÂÂ.
â Jeff Schaller
Feb 20 at 3:36
add a comment |Â
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
I think you took the example too literally; theyâÂÂre looking for the output of thetreecommand to be put between the bracketed text, not a hard-coded replacement after file1.txt of âÂÂnewfileâÂÂ.
â Jeff Schaller
Feb 20 at 3:36
add a comment |Â
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
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
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 thetreecommand to be put between the bracketed text, not a hard-coded replacement after file1.txt of âÂÂnewfileâÂÂ.
â Jeff Schaller
Feb 20 at 3:36
add a comment |Â
I think you took the example too literally; theyâÂÂre looking for the output of thetreecommand 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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password