write variable into specific line with sed command
Clash Royale CLAN TAG#URR8PPP
echo -n "##### STATIONS? #######"
read station
text="stab_site "
I need to write echo $text$station
into the 27th line of text file. I used below command but it didn't work,
sed -i "27i$text$station" text.data
it produce; $textankr ista
"text" variable cannot be passed within sed function.
sed
add a comment |
echo -n "##### STATIONS? #######"
read station
text="stab_site "
I need to write echo $text$station
into the 27th line of text file. I used below command but it didn't work,
sed -i "27i$text$station" text.data
it produce; $textankr ista
"text" variable cannot be passed within sed function.
sed
add a comment |
echo -n "##### STATIONS? #######"
read station
text="stab_site "
I need to write echo $text$station
into the 27th line of text file. I used below command but it didn't work,
sed -i "27i$text$station" text.data
it produce; $textankr ista
"text" variable cannot be passed within sed function.
sed
echo -n "##### STATIONS? #######"
read station
text="stab_site "
I need to write echo $text$station
into the 27th line of text file. I used below command but it didn't work,
sed -i "27i$text$station" text.data
it produce; $textankr ista
"text" variable cannot be passed within sed function.
sed
sed
asked Dec 13 '15 at 15:45
deepblue_86deepblue_86
12515
12515
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
That's because your shell is interpreting $
to mean a literal (escaped) dollar sign. Try
sed -i "27i\$text$station" text.data
or using a mix of single- and double-quotes
sed -i '27i'"$text$station" text.data
or with a literal newline
sed -i "27i
$text$station" text.data
(which is more POSIXly correct, I think).
how can I give one space after the beginning of the written line?
– deepblue_86
Dec 13 '15 at 16:36
@deepblue_86 where exactly? You should be able to add a literal space anywhere in the replacement text e.g. ` $text$station` or$text $station
or$text$station
– steeldriver
Dec 13 '15 at 16:42
add a comment |
Maybe the problem can be a empty output file.
Example:
#!/bin/bash
CAMINHO='/etc/scripts/dropbox.log'
RESULTADO=`date +%d/%m/%Y | tr 'n' ' '; dropbox status;`
sed -i "1i
$RESULTADO" /etc/scripts/dropbox.log
This script cant write to a empty file dropbox.log.
Write some text on file and execute script again.
The problem was solved with the other answer; why would it be caused by an empty output file?
– Stephen Kitt
Jan 14 at 13:39
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f249095%2fwrite-variable-into-specific-line-with-sed-command%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
That's because your shell is interpreting $
to mean a literal (escaped) dollar sign. Try
sed -i "27i\$text$station" text.data
or using a mix of single- and double-quotes
sed -i '27i'"$text$station" text.data
or with a literal newline
sed -i "27i
$text$station" text.data
(which is more POSIXly correct, I think).
how can I give one space after the beginning of the written line?
– deepblue_86
Dec 13 '15 at 16:36
@deepblue_86 where exactly? You should be able to add a literal space anywhere in the replacement text e.g. ` $text$station` or$text $station
or$text$station
– steeldriver
Dec 13 '15 at 16:42
add a comment |
That's because your shell is interpreting $
to mean a literal (escaped) dollar sign. Try
sed -i "27i\$text$station" text.data
or using a mix of single- and double-quotes
sed -i '27i'"$text$station" text.data
or with a literal newline
sed -i "27i
$text$station" text.data
(which is more POSIXly correct, I think).
how can I give one space after the beginning of the written line?
– deepblue_86
Dec 13 '15 at 16:36
@deepblue_86 where exactly? You should be able to add a literal space anywhere in the replacement text e.g. ` $text$station` or$text $station
or$text$station
– steeldriver
Dec 13 '15 at 16:42
add a comment |
That's because your shell is interpreting $
to mean a literal (escaped) dollar sign. Try
sed -i "27i\$text$station" text.data
or using a mix of single- and double-quotes
sed -i '27i'"$text$station" text.data
or with a literal newline
sed -i "27i
$text$station" text.data
(which is more POSIXly correct, I think).
That's because your shell is interpreting $
to mean a literal (escaped) dollar sign. Try
sed -i "27i\$text$station" text.data
or using a mix of single- and double-quotes
sed -i '27i'"$text$station" text.data
or with a literal newline
sed -i "27i
$text$station" text.data
(which is more POSIXly correct, I think).
answered Dec 13 '15 at 16:00
steeldriversteeldriver
35.8k35286
35.8k35286
how can I give one space after the beginning of the written line?
– deepblue_86
Dec 13 '15 at 16:36
@deepblue_86 where exactly? You should be able to add a literal space anywhere in the replacement text e.g. ` $text$station` or$text $station
or$text$station
– steeldriver
Dec 13 '15 at 16:42
add a comment |
how can I give one space after the beginning of the written line?
– deepblue_86
Dec 13 '15 at 16:36
@deepblue_86 where exactly? You should be able to add a literal space anywhere in the replacement text e.g. ` $text$station` or$text $station
or$text$station
– steeldriver
Dec 13 '15 at 16:42
how can I give one space after the beginning of the written line?
– deepblue_86
Dec 13 '15 at 16:36
how can I give one space after the beginning of the written line?
– deepblue_86
Dec 13 '15 at 16:36
@deepblue_86 where exactly? You should be able to add a literal space anywhere in the replacement text e.g. ` $text$station` or
$text $station
or $text$station
– steeldriver
Dec 13 '15 at 16:42
@deepblue_86 where exactly? You should be able to add a literal space anywhere in the replacement text e.g. ` $text$station` or
$text $station
or $text$station
– steeldriver
Dec 13 '15 at 16:42
add a comment |
Maybe the problem can be a empty output file.
Example:
#!/bin/bash
CAMINHO='/etc/scripts/dropbox.log'
RESULTADO=`date +%d/%m/%Y | tr 'n' ' '; dropbox status;`
sed -i "1i
$RESULTADO" /etc/scripts/dropbox.log
This script cant write to a empty file dropbox.log.
Write some text on file and execute script again.
The problem was solved with the other answer; why would it be caused by an empty output file?
– Stephen Kitt
Jan 14 at 13:39
add a comment |
Maybe the problem can be a empty output file.
Example:
#!/bin/bash
CAMINHO='/etc/scripts/dropbox.log'
RESULTADO=`date +%d/%m/%Y | tr 'n' ' '; dropbox status;`
sed -i "1i
$RESULTADO" /etc/scripts/dropbox.log
This script cant write to a empty file dropbox.log.
Write some text on file and execute script again.
The problem was solved with the other answer; why would it be caused by an empty output file?
– Stephen Kitt
Jan 14 at 13:39
add a comment |
Maybe the problem can be a empty output file.
Example:
#!/bin/bash
CAMINHO='/etc/scripts/dropbox.log'
RESULTADO=`date +%d/%m/%Y | tr 'n' ' '; dropbox status;`
sed -i "1i
$RESULTADO" /etc/scripts/dropbox.log
This script cant write to a empty file dropbox.log.
Write some text on file and execute script again.
Maybe the problem can be a empty output file.
Example:
#!/bin/bash
CAMINHO='/etc/scripts/dropbox.log'
RESULTADO=`date +%d/%m/%Y | tr 'n' ' '; dropbox status;`
sed -i "1i
$RESULTADO" /etc/scripts/dropbox.log
This script cant write to a empty file dropbox.log.
Write some text on file and execute script again.
answered Jan 14 at 13:25
Lucas CataniLucas Catani
11
11
The problem was solved with the other answer; why would it be caused by an empty output file?
– Stephen Kitt
Jan 14 at 13:39
add a comment |
The problem was solved with the other answer; why would it be caused by an empty output file?
– Stephen Kitt
Jan 14 at 13:39
The problem was solved with the other answer; why would it be caused by an empty output file?
– Stephen Kitt
Jan 14 at 13:39
The problem was solved with the other answer; why would it be caused by an empty output file?
– Stephen Kitt
Jan 14 at 13:39
add a comment |
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.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f249095%2fwrite-variable-into-specific-line-with-sed-command%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
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