sub-shell inside sed
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I can append to the start of a file fine with:
sed -i '1s/^/wordn/' file
I'm reading that if I use double quotes I can expand variables, so I try:
sed -i "1s/^/$(printenv)n/" file
I end up getting back:
sed: -e expression #1, char 15: unterminated `s' command
What is happening here. Is it related to the contents of the variable or something else?
text-processing sed
add a comment |Â
up vote
1
down vote
favorite
I can append to the start of a file fine with:
sed -i '1s/^/wordn/' file
I'm reading that if I use double quotes I can expand variables, so I try:
sed -i "1s/^/$(printenv)n/" file
I end up getting back:
sed: -e expression #1, char 15: unterminated `s' command
What is happening here. Is it related to the contents of the variable or something else?
text-processing sed
$(command)
is not a variable...
â don_crissti
Oct 14 '17 at 15:27
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I can append to the start of a file fine with:
sed -i '1s/^/wordn/' file
I'm reading that if I use double quotes I can expand variables, so I try:
sed -i "1s/^/$(printenv)n/" file
I end up getting back:
sed: -e expression #1, char 15: unterminated `s' command
What is happening here. Is it related to the contents of the variable or something else?
text-processing sed
I can append to the start of a file fine with:
sed -i '1s/^/wordn/' file
I'm reading that if I use double quotes I can expand variables, so I try:
sed -i "1s/^/$(printenv)n/" file
I end up getting back:
sed: -e expression #1, char 15: unterminated `s' command
What is happening here. Is it related to the contents of the variable or something else?
text-processing sed
edited Oct 14 '17 at 15:26
don_crissti
47k15124154
47k15124154
asked Oct 14 '17 at 14:56
Philip Kirkbride
2,3022470
2,3022470
$(command)
is not a variable...
â don_crissti
Oct 14 '17 at 15:27
add a comment |Â
$(command)
is not a variable...
â don_crissti
Oct 14 '17 at 15:27
$(command)
is not a variable...â don_crissti
Oct 14 '17 at 15:27
$(command)
is not a variable...â don_crissti
Oct 14 '17 at 15:27
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
6
down vote
accepted
I think the following would work:
sed -i '1 e printenv' file
From the GNU sed manual:
'e COMMAND'
Executes COMMAND and sends its output to the output stream. The
command can run across multiple lines, all but the last ending with
a back-slash.
Alternatively, you can use cat
, but this requires creating a temporary file:
cat <(printenv) file > temporary_file; mv temporary_file file
If the package moreutils
is installed on your machine, you can avoid creating a temporary file manually by using sponge
:
cat <(printenv) file | sponge file
add a comment |Â
up vote
5
down vote
To insert the contents before line 1:
ed -s file <<< $'0r !printenvnwq'
To insert the contents after line 1:
ed -s file <<< $'1r !printenvnwq'
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
I think the following would work:
sed -i '1 e printenv' file
From the GNU sed manual:
'e COMMAND'
Executes COMMAND and sends its output to the output stream. The
command can run across multiple lines, all but the last ending with
a back-slash.
Alternatively, you can use cat
, but this requires creating a temporary file:
cat <(printenv) file > temporary_file; mv temporary_file file
If the package moreutils
is installed on your machine, you can avoid creating a temporary file manually by using sponge
:
cat <(printenv) file | sponge file
add a comment |Â
up vote
6
down vote
accepted
I think the following would work:
sed -i '1 e printenv' file
From the GNU sed manual:
'e COMMAND'
Executes COMMAND and sends its output to the output stream. The
command can run across multiple lines, all but the last ending with
a back-slash.
Alternatively, you can use cat
, but this requires creating a temporary file:
cat <(printenv) file > temporary_file; mv temporary_file file
If the package moreutils
is installed on your machine, you can avoid creating a temporary file manually by using sponge
:
cat <(printenv) file | sponge file
add a comment |Â
up vote
6
down vote
accepted
up vote
6
down vote
accepted
I think the following would work:
sed -i '1 e printenv' file
From the GNU sed manual:
'e COMMAND'
Executes COMMAND and sends its output to the output stream. The
command can run across multiple lines, all but the last ending with
a back-slash.
Alternatively, you can use cat
, but this requires creating a temporary file:
cat <(printenv) file > temporary_file; mv temporary_file file
If the package moreutils
is installed on your machine, you can avoid creating a temporary file manually by using sponge
:
cat <(printenv) file | sponge file
I think the following would work:
sed -i '1 e printenv' file
From the GNU sed manual:
'e COMMAND'
Executes COMMAND and sends its output to the output stream. The
command can run across multiple lines, all but the last ending with
a back-slash.
Alternatively, you can use cat
, but this requires creating a temporary file:
cat <(printenv) file > temporary_file; mv temporary_file file
If the package moreutils
is installed on your machine, you can avoid creating a temporary file manually by using sponge
:
cat <(printenv) file | sponge file
edited Oct 14 '17 at 16:58
answered Oct 14 '17 at 15:11
Rastapopoulos
518112
518112
add a comment |Â
add a comment |Â
up vote
5
down vote
To insert the contents before line 1:
ed -s file <<< $'0r !printenvnwq'
To insert the contents after line 1:
ed -s file <<< $'1r !printenvnwq'
add a comment |Â
up vote
5
down vote
To insert the contents before line 1:
ed -s file <<< $'0r !printenvnwq'
To insert the contents after line 1:
ed -s file <<< $'1r !printenvnwq'
add a comment |Â
up vote
5
down vote
up vote
5
down vote
To insert the contents before line 1:
ed -s file <<< $'0r !printenvnwq'
To insert the contents after line 1:
ed -s file <<< $'1r !printenvnwq'
To insert the contents before line 1:
ed -s file <<< $'0r !printenvnwq'
To insert the contents after line 1:
ed -s file <<< $'1r !printenvnwq'
answered Oct 14 '17 at 15:12
Jeff Schaller
32.1k849109
32.1k849109
add a comment |Â
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%2f398118%2fsub-shell-inside-sed%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
$(command)
is not a variable...â don_crissti
Oct 14 '17 at 15:27