How to replace an entire paragraph in a file?











up vote
0
down vote

favorite












I want to change this:



...
%%%23begin
aaaaaaa \
bbbbbbb \
ccccccc \
%%%23end
...


to this



 ...
%%%23begin
xxxxxxxx \
yyyyyyyy \
zzzzzzzzz \
tttttttt \
%%%23end
...


the number of lines might change after editing.



using this function :



function editEntry(){
local entryIndex="$1"
local pattern='^%%%'$entryIndex'begin'
d1=$(grep -n "$pattern" "$theBook" | cut -d: -f 1)
d1=$((d1+1))
local pattern='^%%%'$entryIndex'end'
d2=$(grep -n "$pattern" "$theBook" | cut -d: -f 1)
d2=$((d2-1))
local text1=$(sed -n "$d1,$d2"p "$theBook")
echo "$text1" >| "$tmpfile"
gedit "$tmpfile" && local text2=$(cat "$tmpfile" )
sed -i "s@$text1@$text2@" "$theBook"
}


apparently sed does not change entire paragraphs , it only operates on lines










share|improve this question
























  • What change do you want to make? It looks like you want to operate on the lines between 23begin and 23end, is that right? Please edit your question and show us your desired output so we can understand what you need.
    – terdon
    Dec 3 at 19:10










  • yes that's what I want and sed could not do the job
    – Neo Mosaid
    Dec 3 at 19:15










  • In the expected answer do you still want to be able to manually edit $text1 into $text2? Which would mean that you are just looking for an easier way to extract the text between 23begin and 23end, right?
    – nohillside
    Dec 3 at 19:20










  • actually I can extract it, I want an easy way to replace it after I have edited it
    – Neo Mosaid
    Dec 3 at 19:23















up vote
0
down vote

favorite












I want to change this:



...
%%%23begin
aaaaaaa \
bbbbbbb \
ccccccc \
%%%23end
...


to this



 ...
%%%23begin
xxxxxxxx \
yyyyyyyy \
zzzzzzzzz \
tttttttt \
%%%23end
...


the number of lines might change after editing.



using this function :



function editEntry(){
local entryIndex="$1"
local pattern='^%%%'$entryIndex'begin'
d1=$(grep -n "$pattern" "$theBook" | cut -d: -f 1)
d1=$((d1+1))
local pattern='^%%%'$entryIndex'end'
d2=$(grep -n "$pattern" "$theBook" | cut -d: -f 1)
d2=$((d2-1))
local text1=$(sed -n "$d1,$d2"p "$theBook")
echo "$text1" >| "$tmpfile"
gedit "$tmpfile" && local text2=$(cat "$tmpfile" )
sed -i "s@$text1@$text2@" "$theBook"
}


apparently sed does not change entire paragraphs , it only operates on lines










share|improve this question
























  • What change do you want to make? It looks like you want to operate on the lines between 23begin and 23end, is that right? Please edit your question and show us your desired output so we can understand what you need.
    – terdon
    Dec 3 at 19:10










  • yes that's what I want and sed could not do the job
    – Neo Mosaid
    Dec 3 at 19:15










  • In the expected answer do you still want to be able to manually edit $text1 into $text2? Which would mean that you are just looking for an easier way to extract the text between 23begin and 23end, right?
    – nohillside
    Dec 3 at 19:20










  • actually I can extract it, I want an easy way to replace it after I have edited it
    – Neo Mosaid
    Dec 3 at 19:23













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to change this:



...
%%%23begin
aaaaaaa \
bbbbbbb \
ccccccc \
%%%23end
...


to this



 ...
%%%23begin
xxxxxxxx \
yyyyyyyy \
zzzzzzzzz \
tttttttt \
%%%23end
...


the number of lines might change after editing.



using this function :



function editEntry(){
local entryIndex="$1"
local pattern='^%%%'$entryIndex'begin'
d1=$(grep -n "$pattern" "$theBook" | cut -d: -f 1)
d1=$((d1+1))
local pattern='^%%%'$entryIndex'end'
d2=$(grep -n "$pattern" "$theBook" | cut -d: -f 1)
d2=$((d2-1))
local text1=$(sed -n "$d1,$d2"p "$theBook")
echo "$text1" >| "$tmpfile"
gedit "$tmpfile" && local text2=$(cat "$tmpfile" )
sed -i "s@$text1@$text2@" "$theBook"
}


apparently sed does not change entire paragraphs , it only operates on lines










share|improve this question















I want to change this:



...
%%%23begin
aaaaaaa \
bbbbbbb \
ccccccc \
%%%23end
...


to this



 ...
%%%23begin
xxxxxxxx \
yyyyyyyy \
zzzzzzzzz \
tttttttt \
%%%23end
...


the number of lines might change after editing.



using this function :



function editEntry(){
local entryIndex="$1"
local pattern='^%%%'$entryIndex'begin'
d1=$(grep -n "$pattern" "$theBook" | cut -d: -f 1)
d1=$((d1+1))
local pattern='^%%%'$entryIndex'end'
d2=$(grep -n "$pattern" "$theBook" | cut -d: -f 1)
d2=$((d2-1))
local text1=$(sed -n "$d1,$d2"p "$theBook")
echo "$text1" >| "$tmpfile"
gedit "$tmpfile" && local text2=$(cat "$tmpfile" )
sed -i "s@$text1@$text2@" "$theBook"
}


apparently sed does not change entire paragraphs , it only operates on lines







text-processing sed scripting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 3 at 19:32









Jeff Schaller

37.9k1053123




37.9k1053123










asked Dec 3 at 19:07









Neo Mosaid

3216




3216












  • What change do you want to make? It looks like you want to operate on the lines between 23begin and 23end, is that right? Please edit your question and show us your desired output so we can understand what you need.
    – terdon
    Dec 3 at 19:10










  • yes that's what I want and sed could not do the job
    – Neo Mosaid
    Dec 3 at 19:15










  • In the expected answer do you still want to be able to manually edit $text1 into $text2? Which would mean that you are just looking for an easier way to extract the text between 23begin and 23end, right?
    – nohillside
    Dec 3 at 19:20










  • actually I can extract it, I want an easy way to replace it after I have edited it
    – Neo Mosaid
    Dec 3 at 19:23


















  • What change do you want to make? It looks like you want to operate on the lines between 23begin and 23end, is that right? Please edit your question and show us your desired output so we can understand what you need.
    – terdon
    Dec 3 at 19:10










  • yes that's what I want and sed could not do the job
    – Neo Mosaid
    Dec 3 at 19:15










  • In the expected answer do you still want to be able to manually edit $text1 into $text2? Which would mean that you are just looking for an easier way to extract the text between 23begin and 23end, right?
    – nohillside
    Dec 3 at 19:20










  • actually I can extract it, I want an easy way to replace it after I have edited it
    – Neo Mosaid
    Dec 3 at 19:23
















What change do you want to make? It looks like you want to operate on the lines between 23begin and 23end, is that right? Please edit your question and show us your desired output so we can understand what you need.
– terdon
Dec 3 at 19:10




What change do you want to make? It looks like you want to operate on the lines between 23begin and 23end, is that right? Please edit your question and show us your desired output so we can understand what you need.
– terdon
Dec 3 at 19:10












yes that's what I want and sed could not do the job
– Neo Mosaid
Dec 3 at 19:15




yes that's what I want and sed could not do the job
– Neo Mosaid
Dec 3 at 19:15












In the expected answer do you still want to be able to manually edit $text1 into $text2? Which would mean that you are just looking for an easier way to extract the text between 23begin and 23end, right?
– nohillside
Dec 3 at 19:20




In the expected answer do you still want to be able to manually edit $text1 into $text2? Which would mean that you are just looking for an easier way to extract the text between 23begin and 23end, right?
– nohillside
Dec 3 at 19:20












actually I can extract it, I want an easy way to replace it after I have edited it
– Neo Mosaid
Dec 3 at 19:23




actually I can extract it, I want an easy way to replace it after I have edited it
– Neo Mosaid
Dec 3 at 19:23










1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










Scripting text edits is what ed is for:



ed file <<'SCRIPT'
/%%%23begin/+1,/%%%23end/-1d
/%%%23begin/a
xxxxxxxx \
yyyyyyyy \
zzzzzzzzz \
tttttttt \
.
wq
SCRIPT


First, delete the contents between (but not including) the markers, then append the desired text after the start marker. "Dot" ends input. Save and quit.






share|improve this answer





















  • the function above is a part of a lager bash script that makes the file, and it contains arabic characters that's way I used gedit.
    – Neo Mosaid
    Dec 3 at 19:29










  • here is what I did based on your answer ` printf " /$pattern1/+1,/$pattern2/-1d n /$pattern1/a $text2 n . n wq n SCRIPT " | ed -s "$theBook" ` but it didn't work
    – Neo Mosaid
    Dec 3 at 19:55










  • If you want to use printf: printf '%sn' "/$pattern1/+1,/$pattern2/-1d" "/$pattern1/a" "$text2" . wq | ed -s "$theBook" -- the "SCRIPT" is the heredoc terminator, not an ed command.
    – glenn jackman
    Dec 3 at 20:23












  • thank you so much
    – Neo Mosaid
    Dec 3 at 20:47











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%2f485750%2fhow-to-replace-an-entire-paragraph-in-a-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










Scripting text edits is what ed is for:



ed file <<'SCRIPT'
/%%%23begin/+1,/%%%23end/-1d
/%%%23begin/a
xxxxxxxx \
yyyyyyyy \
zzzzzzzzz \
tttttttt \
.
wq
SCRIPT


First, delete the contents between (but not including) the markers, then append the desired text after the start marker. "Dot" ends input. Save and quit.






share|improve this answer





















  • the function above is a part of a lager bash script that makes the file, and it contains arabic characters that's way I used gedit.
    – Neo Mosaid
    Dec 3 at 19:29










  • here is what I did based on your answer ` printf " /$pattern1/+1,/$pattern2/-1d n /$pattern1/a $text2 n . n wq n SCRIPT " | ed -s "$theBook" ` but it didn't work
    – Neo Mosaid
    Dec 3 at 19:55










  • If you want to use printf: printf '%sn' "/$pattern1/+1,/$pattern2/-1d" "/$pattern1/a" "$text2" . wq | ed -s "$theBook" -- the "SCRIPT" is the heredoc terminator, not an ed command.
    – glenn jackman
    Dec 3 at 20:23












  • thank you so much
    – Neo Mosaid
    Dec 3 at 20:47















up vote
3
down vote



accepted










Scripting text edits is what ed is for:



ed file <<'SCRIPT'
/%%%23begin/+1,/%%%23end/-1d
/%%%23begin/a
xxxxxxxx \
yyyyyyyy \
zzzzzzzzz \
tttttttt \
.
wq
SCRIPT


First, delete the contents between (but not including) the markers, then append the desired text after the start marker. "Dot" ends input. Save and quit.






share|improve this answer





















  • the function above is a part of a lager bash script that makes the file, and it contains arabic characters that's way I used gedit.
    – Neo Mosaid
    Dec 3 at 19:29










  • here is what I did based on your answer ` printf " /$pattern1/+1,/$pattern2/-1d n /$pattern1/a $text2 n . n wq n SCRIPT " | ed -s "$theBook" ` but it didn't work
    – Neo Mosaid
    Dec 3 at 19:55










  • If you want to use printf: printf '%sn' "/$pattern1/+1,/$pattern2/-1d" "/$pattern1/a" "$text2" . wq | ed -s "$theBook" -- the "SCRIPT" is the heredoc terminator, not an ed command.
    – glenn jackman
    Dec 3 at 20:23












  • thank you so much
    – Neo Mosaid
    Dec 3 at 20:47













up vote
3
down vote



accepted







up vote
3
down vote



accepted






Scripting text edits is what ed is for:



ed file <<'SCRIPT'
/%%%23begin/+1,/%%%23end/-1d
/%%%23begin/a
xxxxxxxx \
yyyyyyyy \
zzzzzzzzz \
tttttttt \
.
wq
SCRIPT


First, delete the contents between (but not including) the markers, then append the desired text after the start marker. "Dot" ends input. Save and quit.






share|improve this answer












Scripting text edits is what ed is for:



ed file <<'SCRIPT'
/%%%23begin/+1,/%%%23end/-1d
/%%%23begin/a
xxxxxxxx \
yyyyyyyy \
zzzzzzzzz \
tttttttt \
.
wq
SCRIPT


First, delete the contents between (but not including) the markers, then append the desired text after the start marker. "Dot" ends input. Save and quit.







share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 3 at 19:21









glenn jackman

50k569106




50k569106












  • the function above is a part of a lager bash script that makes the file, and it contains arabic characters that's way I used gedit.
    – Neo Mosaid
    Dec 3 at 19:29










  • here is what I did based on your answer ` printf " /$pattern1/+1,/$pattern2/-1d n /$pattern1/a $text2 n . n wq n SCRIPT " | ed -s "$theBook" ` but it didn't work
    – Neo Mosaid
    Dec 3 at 19:55










  • If you want to use printf: printf '%sn' "/$pattern1/+1,/$pattern2/-1d" "/$pattern1/a" "$text2" . wq | ed -s "$theBook" -- the "SCRIPT" is the heredoc terminator, not an ed command.
    – glenn jackman
    Dec 3 at 20:23












  • thank you so much
    – Neo Mosaid
    Dec 3 at 20:47


















  • the function above is a part of a lager bash script that makes the file, and it contains arabic characters that's way I used gedit.
    – Neo Mosaid
    Dec 3 at 19:29










  • here is what I did based on your answer ` printf " /$pattern1/+1,/$pattern2/-1d n /$pattern1/a $text2 n . n wq n SCRIPT " | ed -s "$theBook" ` but it didn't work
    – Neo Mosaid
    Dec 3 at 19:55










  • If you want to use printf: printf '%sn' "/$pattern1/+1,/$pattern2/-1d" "/$pattern1/a" "$text2" . wq | ed -s "$theBook" -- the "SCRIPT" is the heredoc terminator, not an ed command.
    – glenn jackman
    Dec 3 at 20:23












  • thank you so much
    – Neo Mosaid
    Dec 3 at 20:47
















the function above is a part of a lager bash script that makes the file, and it contains arabic characters that's way I used gedit.
– Neo Mosaid
Dec 3 at 19:29




the function above is a part of a lager bash script that makes the file, and it contains arabic characters that's way I used gedit.
– Neo Mosaid
Dec 3 at 19:29












here is what I did based on your answer ` printf " /$pattern1/+1,/$pattern2/-1d n /$pattern1/a $text2 n . n wq n SCRIPT " | ed -s "$theBook" ` but it didn't work
– Neo Mosaid
Dec 3 at 19:55




here is what I did based on your answer ` printf " /$pattern1/+1,/$pattern2/-1d n /$pattern1/a $text2 n . n wq n SCRIPT " | ed -s "$theBook" ` but it didn't work
– Neo Mosaid
Dec 3 at 19:55












If you want to use printf: printf '%sn' "/$pattern1/+1,/$pattern2/-1d" "/$pattern1/a" "$text2" . wq | ed -s "$theBook" -- the "SCRIPT" is the heredoc terminator, not an ed command.
– glenn jackman
Dec 3 at 20:23






If you want to use printf: printf '%sn' "/$pattern1/+1,/$pattern2/-1d" "/$pattern1/a" "$text2" . wq | ed -s "$theBook" -- the "SCRIPT" is the heredoc terminator, not an ed command.
– glenn jackman
Dec 3 at 20:23














thank you so much
– Neo Mosaid
Dec 3 at 20:47




thank you so much
– Neo Mosaid
Dec 3 at 20:47


















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%2f485750%2fhow-to-replace-an-entire-paragraph-in-a-file%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

Morgemoulin

Scott Moir

Souastre