sed in bash script with variables returns unterminated error
Clash 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
sed
add a comment |Â
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
sed
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 variableappend=$(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 a comment |Â
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
sed
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
sed
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 variableappend=$(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 a comment |Â
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 variableappend=$(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
add a comment |Â
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).
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
add a comment |Â
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).
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
add a comment |Â
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).
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
add a comment |Â
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).
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).
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
add a comment |Â
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
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%2f457208%2fsed-in-bash-script-with-variables-returns-unterminated-error%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
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