How to insert multiple lines with sed
I want to add this
#this
##is my
text
before the line
the specific line
I tried this
sed -i '/the specific line/i
#this
##is my
text
' text.txt
but it only adds in 'text'.
I also tried various combinations with and
" "
but nothing worked.
bash sed
add a comment |
I want to add this
#this
##is my
text
before the line
the specific line
I tried this
sed -i '/the specific line/i
#this
##is my
text
' text.txt
but it only adds in 'text'.
I also tried various combinations with and
" "
but nothing worked.
bash sed
add a comment |
I want to add this
#this
##is my
text
before the line
the specific line
I tried this
sed -i '/the specific line/i
#this
##is my
text
' text.txt
but it only adds in 'text'.
I also tried various combinations with and
" "
but nothing worked.
bash sed
I want to add this
#this
##is my
text
before the line
the specific line
I tried this
sed -i '/the specific line/i
#this
##is my
text
' text.txt
but it only adds in 'text'.
I also tried various combinations with and
" "
but nothing worked.
bash sed
bash sed
edited Nov 26 '15 at 8:54
kos
25.5k870121
25.5k870121
asked Nov 26 '15 at 7:33
Paul Bernhard WagnerPaul Bernhard Wagner
2152511
2152511
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
With newlines:
% sed -i '/the specific line/i #thisn##is myntext' foo
% cat foo
#this
##is my
text
the specific line
add a comment |
You're missing the trailing backslash at the end of some lines (and you have an eccessive newline at the end of the last line you want to insert):
sed -i '/the specific line/i
#this
##is my
text' file
% cat file
foo
the specific line
bar
% sed -i '/the specific line/i
#this
##is my
text' file
% cat file
foo
#this
##is my
text
the specific line
bar
add a comment |
When the replacement string has newlines and spaces, you can use something else. We will try to insert the output of ls -l
in the middle of some template file.
awk 'NR==FNR {a[NR]=$0;next}
/Insert index here/ {for (i=1; i <= length(a); i++) { print a[i] }}
{print}'
<(ls -l) text.txt
When you want something inserted after a line, you can move the command {print}
or switch to :
sed '/Insert command output after this line/r'<(ls -l) text.txt
You can also use sed for inserting before a line with
sed 's/Insert command output after this line/ls -l; echo "&"/e' text.txt
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2faskubuntu.com%2fquestions%2f702677%2fhow-to-insert-multiple-lines-with-sed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
With newlines:
% sed -i '/the specific line/i #thisn##is myntext' foo
% cat foo
#this
##is my
text
the specific line
add a comment |
With newlines:
% sed -i '/the specific line/i #thisn##is myntext' foo
% cat foo
#this
##is my
text
the specific line
add a comment |
With newlines:
% sed -i '/the specific line/i #thisn##is myntext' foo
% cat foo
#this
##is my
text
the specific line
With newlines:
% sed -i '/the specific line/i #thisn##is myntext' foo
% cat foo
#this
##is my
text
the specific line
answered Nov 26 '15 at 8:14
A.B.A.B.
68.5k12168258
68.5k12168258
add a comment |
add a comment |
You're missing the trailing backslash at the end of some lines (and you have an eccessive newline at the end of the last line you want to insert):
sed -i '/the specific line/i
#this
##is my
text' file
% cat file
foo
the specific line
bar
% sed -i '/the specific line/i
#this
##is my
text' file
% cat file
foo
#this
##is my
text
the specific line
bar
add a comment |
You're missing the trailing backslash at the end of some lines (and you have an eccessive newline at the end of the last line you want to insert):
sed -i '/the specific line/i
#this
##is my
text' file
% cat file
foo
the specific line
bar
% sed -i '/the specific line/i
#this
##is my
text' file
% cat file
foo
#this
##is my
text
the specific line
bar
add a comment |
You're missing the trailing backslash at the end of some lines (and you have an eccessive newline at the end of the last line you want to insert):
sed -i '/the specific line/i
#this
##is my
text' file
% cat file
foo
the specific line
bar
% sed -i '/the specific line/i
#this
##is my
text' file
% cat file
foo
#this
##is my
text
the specific line
bar
You're missing the trailing backslash at the end of some lines (and you have an eccessive newline at the end of the last line you want to insert):
sed -i '/the specific line/i
#this
##is my
text' file
% cat file
foo
the specific line
bar
% sed -i '/the specific line/i
#this
##is my
text' file
% cat file
foo
#this
##is my
text
the specific line
bar
edited Nov 26 '15 at 8:51
A.B.
68.5k12168258
68.5k12168258
answered Nov 26 '15 at 8:29
koskos
25.5k870121
25.5k870121
add a comment |
add a comment |
When the replacement string has newlines and spaces, you can use something else. We will try to insert the output of ls -l
in the middle of some template file.
awk 'NR==FNR {a[NR]=$0;next}
/Insert index here/ {for (i=1; i <= length(a); i++) { print a[i] }}
{print}'
<(ls -l) text.txt
When you want something inserted after a line, you can move the command {print}
or switch to :
sed '/Insert command output after this line/r'<(ls -l) text.txt
You can also use sed for inserting before a line with
sed 's/Insert command output after this line/ls -l; echo "&"/e' text.txt
add a comment |
When the replacement string has newlines and spaces, you can use something else. We will try to insert the output of ls -l
in the middle of some template file.
awk 'NR==FNR {a[NR]=$0;next}
/Insert index here/ {for (i=1; i <= length(a); i++) { print a[i] }}
{print}'
<(ls -l) text.txt
When you want something inserted after a line, you can move the command {print}
or switch to :
sed '/Insert command output after this line/r'<(ls -l) text.txt
You can also use sed for inserting before a line with
sed 's/Insert command output after this line/ls -l; echo "&"/e' text.txt
add a comment |
When the replacement string has newlines and spaces, you can use something else. We will try to insert the output of ls -l
in the middle of some template file.
awk 'NR==FNR {a[NR]=$0;next}
/Insert index here/ {for (i=1; i <= length(a); i++) { print a[i] }}
{print}'
<(ls -l) text.txt
When you want something inserted after a line, you can move the command {print}
or switch to :
sed '/Insert command output after this line/r'<(ls -l) text.txt
You can also use sed for inserting before a line with
sed 's/Insert command output after this line/ls -l; echo "&"/e' text.txt
When the replacement string has newlines and spaces, you can use something else. We will try to insert the output of ls -l
in the middle of some template file.
awk 'NR==FNR {a[NR]=$0;next}
/Insert index here/ {for (i=1; i <= length(a); i++) { print a[i] }}
{print}'
<(ls -l) text.txt
When you want something inserted after a line, you can move the command {print}
or switch to :
sed '/Insert command output after this line/r'<(ls -l) text.txt
You can also use sed for inserting before a line with
sed 's/Insert command output after this line/ls -l; echo "&"/e' text.txt
edited Jan 2 at 17:05
answered Dec 31 '18 at 11:42
Walter AWalter A
1113
1113
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- 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%2faskubuntu.com%2fquestions%2f702677%2fhow-to-insert-multiple-lines-with-sed%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