sed in bash script with variables returns unterminated error

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
-1
down vote

favorite












I need to replace the first line of a file with the complete content from another file. Does on see what I'm missing?



$ cat old.txt
---
foo
bar

$ cat append.txt
---
123
<321>

$ cat process.sh
old=old.txt
append=append.txt
sed -i "1s/.*/$append/" $old
cat $old


I'd expect



$ bash process.sh
---
123
<321>
foo
bar


Though instead I got



sed: -e expression #1, char 9: unterminated `s' command


when trying to sed with a variable inside my script



while this one works (without variable)
sed -i "1s/.*/loremipsum/" $old







share|improve this question





















  • Add quotations around the $append variable name.
    – Raman Sailopal
    Jul 19 at 12:46










  • @RamanSailopal Thanks. Either: sed -i "1s/.*/"$append"/" $old nor this helps: sed -i '1s/.*/"$append"/' $old
    – AikenCura
    Jul 19 at 12:56











  • @AikITYM the shell variable append=$(cat append.txt) . Just so that you know why you are getting this error is because, sed doesn't like newlines on the RHS of a s/// command.
    – Rakesh Sharma
    Jul 20 at 5:25
















up vote
-1
down vote

favorite












I need to replace the first line of a file with the complete content from another file. Does on see what I'm missing?



$ cat old.txt
---
foo
bar

$ cat append.txt
---
123
<321>

$ cat process.sh
old=old.txt
append=append.txt
sed -i "1s/.*/$append/" $old
cat $old


I'd expect



$ bash process.sh
---
123
<321>
foo
bar


Though instead I got



sed: -e expression #1, char 9: unterminated `s' command


when trying to sed with a variable inside my script



while this one works (without variable)
sed -i "1s/.*/loremipsum/" $old







share|improve this question





















  • Add quotations around the $append variable name.
    – Raman Sailopal
    Jul 19 at 12:46










  • @RamanSailopal Thanks. Either: sed -i "1s/.*/"$append"/" $old nor this helps: sed -i '1s/.*/"$append"/' $old
    – AikenCura
    Jul 19 at 12:56











  • @AikITYM the shell variable append=$(cat append.txt) . Just so that you know why you are getting this error is because, sed doesn't like newlines on the RHS of a s/// command.
    – Rakesh Sharma
    Jul 20 at 5:25












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I need to replace the first line of a file with the complete content from another file. Does on see what I'm missing?



$ cat old.txt
---
foo
bar

$ cat append.txt
---
123
<321>

$ cat process.sh
old=old.txt
append=append.txt
sed -i "1s/.*/$append/" $old
cat $old


I'd expect



$ bash process.sh
---
123
<321>
foo
bar


Though instead I got



sed: -e expression #1, char 9: unterminated `s' command


when trying to sed with a variable inside my script



while this one works (without variable)
sed -i "1s/.*/loremipsum/" $old







share|improve this question













I need to replace the first line of a file with the complete content from another file. Does on see what I'm missing?



$ cat old.txt
---
foo
bar

$ cat append.txt
---
123
<321>

$ cat process.sh
old=old.txt
append=append.txt
sed -i "1s/.*/$append/" $old
cat $old


I'd expect



$ bash process.sh
---
123
<321>
foo
bar


Though instead I got



sed: -e expression #1, char 9: unterminated `s' command


when trying to sed with a variable inside my script



while this one works (without variable)
sed -i "1s/.*/loremipsum/" $old









share|improve this question












share|improve this question




share|improve this question








edited Jul 19 at 12:46
























asked Jul 19 at 12:36









AikenCura

11




11











  • Add quotations around the $append variable name.
    – Raman Sailopal
    Jul 19 at 12:46










  • @RamanSailopal Thanks. Either: sed -i "1s/.*/"$append"/" $old nor this helps: sed -i '1s/.*/"$append"/' $old
    – AikenCura
    Jul 19 at 12:56











  • @AikITYM the shell variable append=$(cat append.txt) . Just so that you know why you are getting this error is because, sed doesn't like newlines on the RHS of a s/// command.
    – Rakesh Sharma
    Jul 20 at 5:25
















  • Add quotations around the $append variable name.
    – Raman Sailopal
    Jul 19 at 12:46










  • @RamanSailopal Thanks. Either: sed -i "1s/.*/"$append"/" $old nor this helps: sed -i '1s/.*/"$append"/' $old
    – AikenCura
    Jul 19 at 12:56











  • @AikITYM the shell variable append=$(cat append.txt) . Just so that you know why you are getting this error is because, sed doesn't like newlines on the RHS of a s/// command.
    – Rakesh Sharma
    Jul 20 at 5:25















Add quotations around the $append variable name.
– Raman Sailopal
Jul 19 at 12:46




Add quotations around the $append variable name.
– Raman Sailopal
Jul 19 at 12:46












@RamanSailopal Thanks. Either: sed -i "1s/.*/"$append"/" $old nor this helps: sed -i '1s/.*/"$append"/' $old
– AikenCura
Jul 19 at 12:56





@RamanSailopal Thanks. Either: sed -i "1s/.*/"$append"/" $old nor this helps: sed -i '1s/.*/"$append"/' $old
– AikenCura
Jul 19 at 12:56













@AikITYM the shell variable append=$(cat append.txt) . Just so that you know why you are getting this error is because, sed doesn't like newlines on the RHS of a s/// command.
– Rakesh Sharma
Jul 20 at 5:25




@AikITYM the shell variable append=$(cat append.txt) . Just so that you know why you are getting this error is because, sed doesn't like newlines on the RHS of a s/// command.
– Rakesh Sharma
Jul 20 at 5:25










1 Answer
1






active

oldest

votes

















up vote
2
down vote













To replace the first line of old.txt with the content of append.txt:



$ sed -e '1 r append.txt' -e 'd;' old.txt
---
123
<321>
foo
bar


Add -i before the first -e to do the edit in-place in old.txt.



This runs the following short sed script on the given file:



1 # we're on the first line
r append.txt # read in the whole of the append.txt file
d; # delete the current line

# (implicit print)


On the command line, this is divided into two separate -e expression string since the filename used with the r command must be terminated by a newline (or the end of the current expression string).






share|improve this answer























  • Yes, more or less the same as Replace a line with the full text of a second file
    – don_crissti
    Jul 19 at 13:18










  • @don_crissti Oh, blast from the past.
    – Kusalananda
    Jul 19 at 13:19










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%2f457208%2fsed-in-bash-script-with-variables-returns-unterminated-error%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













To replace the first line of old.txt with the content of append.txt:



$ sed -e '1 r append.txt' -e 'd;' old.txt
---
123
<321>
foo
bar


Add -i before the first -e to do the edit in-place in old.txt.



This runs the following short sed script on the given file:



1 # we're on the first line
r append.txt # read in the whole of the append.txt file
d; # delete the current line

# (implicit print)


On the command line, this is divided into two separate -e expression string since the filename used with the r command must be terminated by a newline (or the end of the current expression string).






share|improve this answer























  • Yes, more or less the same as Replace a line with the full text of a second file
    – don_crissti
    Jul 19 at 13:18










  • @don_crissti Oh, blast from the past.
    – Kusalananda
    Jul 19 at 13:19














up vote
2
down vote













To replace the first line of old.txt with the content of append.txt:



$ sed -e '1 r append.txt' -e 'd;' old.txt
---
123
<321>
foo
bar


Add -i before the first -e to do the edit in-place in old.txt.



This runs the following short sed script on the given file:



1 # we're on the first line
r append.txt # read in the whole of the append.txt file
d; # delete the current line

# (implicit print)


On the command line, this is divided into two separate -e expression string since the filename used with the r command must be terminated by a newline (or the end of the current expression string).






share|improve this answer























  • Yes, more or less the same as Replace a line with the full text of a second file
    – don_crissti
    Jul 19 at 13:18










  • @don_crissti Oh, blast from the past.
    – Kusalananda
    Jul 19 at 13:19












up vote
2
down vote










up vote
2
down vote









To replace the first line of old.txt with the content of append.txt:



$ sed -e '1 r append.txt' -e 'd;' old.txt
---
123
<321>
foo
bar


Add -i before the first -e to do the edit in-place in old.txt.



This runs the following short sed script on the given file:



1 # we're on the first line
r append.txt # read in the whole of the append.txt file
d; # delete the current line

# (implicit print)


On the command line, this is divided into two separate -e expression string since the filename used with the r command must be terminated by a newline (or the end of the current expression string).






share|improve this answer















To replace the first line of old.txt with the content of append.txt:



$ sed -e '1 r append.txt' -e 'd;' old.txt
---
123
<321>
foo
bar


Add -i before the first -e to do the edit in-place in old.txt.



This runs the following short sed script on the given file:



1 # we're on the first line
r append.txt # read in the whole of the append.txt file
d; # delete the current line

# (implicit print)


On the command line, this is divided into two separate -e expression string since the filename used with the r command must be terminated by a newline (or the end of the current expression string).







share|improve this answer















share|improve this answer



share|improve this answer








edited Jul 19 at 13:26


























answered Jul 19 at 12:52









Kusalananda

101k13199311




101k13199311











  • Yes, more or less the same as Replace a line with the full text of a second file
    – don_crissti
    Jul 19 at 13:18










  • @don_crissti Oh, blast from the past.
    – Kusalananda
    Jul 19 at 13:19
















  • Yes, more or less the same as Replace a line with the full text of a second file
    – don_crissti
    Jul 19 at 13:18










  • @don_crissti Oh, blast from the past.
    – Kusalananda
    Jul 19 at 13:19















Yes, more or less the same as Replace a line with the full text of a second file
– don_crissti
Jul 19 at 13:18




Yes, more or less the same as Replace a line with the full text of a second file
– don_crissti
Jul 19 at 13:18












@don_crissti Oh, blast from the past.
– Kusalananda
Jul 19 at 13:19




@don_crissti Oh, blast from the past.
– Kusalananda
Jul 19 at 13:19












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f457208%2fsed-in-bash-script-with-variables-returns-unterminated-error%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

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

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay