Replace using SED + Vars + New Line
I'm trying to use a "sed" with new line, but I want to use this new line in a var like this:
#!/bin/sh
var1='tags'
var2='aw1 n
aw2 n
aw3'
sed -i ':a;N;$!ba;s@'"$var1"'@'"$var2"'@g' *.html
I want to repĺace a pattern tags
in my document to a many lines inside my var2
.
BEFORE:
tags
AFTER
aw1
aw2
aw3
linux command-line bash sed
add a comment |
I'm trying to use a "sed" with new line, but I want to use this new line in a var like this:
#!/bin/sh
var1='tags'
var2='aw1 n
aw2 n
aw3'
sed -i ':a;N;$!ba;s@'"$var1"'@'"$var2"'@g' *.html
I want to repĺace a pattern tags
in my document to a many lines inside my var2
.
BEFORE:
tags
AFTER
aw1
aw2
aw3
linux command-line bash sed
:a;N;$!ba
reads many lines. Do you need this? In your example are those blank lines aftertags
deliberate? As if you wanted to replace many lines with many lines. Butvar1
is not multi-line, so it's not really clear. Please edit the question and clarify.
– Kamil Maciorowski
Feb 28 at 16:41
add a comment |
I'm trying to use a "sed" with new line, but I want to use this new line in a var like this:
#!/bin/sh
var1='tags'
var2='aw1 n
aw2 n
aw3'
sed -i ':a;N;$!ba;s@'"$var1"'@'"$var2"'@g' *.html
I want to repĺace a pattern tags
in my document to a many lines inside my var2
.
BEFORE:
tags
AFTER
aw1
aw2
aw3
linux command-line bash sed
I'm trying to use a "sed" with new line, but I want to use this new line in a var like this:
#!/bin/sh
var1='tags'
var2='aw1 n
aw2 n
aw3'
sed -i ':a;N;$!ba;s@'"$var1"'@'"$var2"'@g' *.html
I want to repĺace a pattern tags
in my document to a many lines inside my var2
.
BEFORE:
tags
AFTER
aw1
aw2
aw3
linux command-line bash sed
linux command-line bash sed
edited Feb 28 at 14:17
Run5k
11.6k73354
11.6k73354
asked Feb 28 at 13:05
Carlos Eduardo BaldocchiCarlos Eduardo Baldocchi
11
11
:a;N;$!ba
reads many lines. Do you need this? In your example are those blank lines aftertags
deliberate? As if you wanted to replace many lines with many lines. Butvar1
is not multi-line, so it's not really clear. Please edit the question and clarify.
– Kamil Maciorowski
Feb 28 at 16:41
add a comment |
:a;N;$!ba
reads many lines. Do you need this? In your example are those blank lines aftertags
deliberate? As if you wanted to replace many lines with many lines. Butvar1
is not multi-line, so it's not really clear. Please edit the question and clarify.
– Kamil Maciorowski
Feb 28 at 16:41
:a;N;$!ba
reads many lines. Do you need this? In your example are those blank lines after tags
deliberate? As if you wanted to replace many lines with many lines. But var1
is not multi-line, so it's not really clear. Please edit the question and clarify.– Kamil Maciorowski
Feb 28 at 16:41
:a;N;$!ba
reads many lines. Do you need this? In your example are those blank lines after tags
deliberate? As if you wanted to replace many lines with many lines. But var1
is not multi-line, so it's not really clear. Please edit the question and clarify.– Kamil Maciorowski
Feb 28 at 16:41
add a comment |
1 Answer
1
active
oldest
votes
If I understood it correctly, it should be as simple as replace every ENTER with "n". Like:
sed "s/tags/aw1naw2naw3/g" file
Do not use both (ENTER and n). Let me know if I understood the problem.
and... I just forgot: If you need to put it in a variable, you should "double escape" the n. Like
v=tags;
c=aw1\naw2\naw3;
sed "s/$v/$c/g" *.html
It’s (IMHO) simpler to put the string in quotes; i.e.,c='aw1naw2naw3'
orc="aw1naw2naw3"
.
– Scott
Mar 1 at 0:15
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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%2fsuperuser.com%2fquestions%2f1410190%2freplace-using-sed-vars-new-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If I understood it correctly, it should be as simple as replace every ENTER with "n". Like:
sed "s/tags/aw1naw2naw3/g" file
Do not use both (ENTER and n). Let me know if I understood the problem.
and... I just forgot: If you need to put it in a variable, you should "double escape" the n. Like
v=tags;
c=aw1\naw2\naw3;
sed "s/$v/$c/g" *.html
It’s (IMHO) simpler to put the string in quotes; i.e.,c='aw1naw2naw3'
orc="aw1naw2naw3"
.
– Scott
Mar 1 at 0:15
add a comment |
If I understood it correctly, it should be as simple as replace every ENTER with "n". Like:
sed "s/tags/aw1naw2naw3/g" file
Do not use both (ENTER and n). Let me know if I understood the problem.
and... I just forgot: If you need to put it in a variable, you should "double escape" the n. Like
v=tags;
c=aw1\naw2\naw3;
sed "s/$v/$c/g" *.html
It’s (IMHO) simpler to put the string in quotes; i.e.,c='aw1naw2naw3'
orc="aw1naw2naw3"
.
– Scott
Mar 1 at 0:15
add a comment |
If I understood it correctly, it should be as simple as replace every ENTER with "n". Like:
sed "s/tags/aw1naw2naw3/g" file
Do not use both (ENTER and n). Let me know if I understood the problem.
and... I just forgot: If you need to put it in a variable, you should "double escape" the n. Like
v=tags;
c=aw1\naw2\naw3;
sed "s/$v/$c/g" *.html
If I understood it correctly, it should be as simple as replace every ENTER with "n". Like:
sed "s/tags/aw1naw2naw3/g" file
Do not use both (ENTER and n). Let me know if I understood the problem.
and... I just forgot: If you need to put it in a variable, you should "double escape" the n. Like
v=tags;
c=aw1\naw2\naw3;
sed "s/$v/$c/g" *.html
answered Feb 28 at 16:08
JuanJuan
1194
1194
It’s (IMHO) simpler to put the string in quotes; i.e.,c='aw1naw2naw3'
orc="aw1naw2naw3"
.
– Scott
Mar 1 at 0:15
add a comment |
It’s (IMHO) simpler to put the string in quotes; i.e.,c='aw1naw2naw3'
orc="aw1naw2naw3"
.
– Scott
Mar 1 at 0:15
It’s (IMHO) simpler to put the string in quotes; i.e.,
c='aw1naw2naw3'
or c="aw1naw2naw3"
.– Scott
Mar 1 at 0:15
It’s (IMHO) simpler to put the string in quotes; i.e.,
c='aw1naw2naw3'
or c="aw1naw2naw3"
.– Scott
Mar 1 at 0:15
add a comment |
Thanks for contributing an answer to Super User!
- 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%2fsuperuser.com%2fquestions%2f1410190%2freplace-using-sed-vars-new-line%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
:a;N;$!ba
reads many lines. Do you need this? In your example are those blank lines aftertags
deliberate? As if you wanted to replace many lines with many lines. Butvar1
is not multi-line, so it's not really clear. Please edit the question and clarify.– Kamil Maciorowski
Feb 28 at 16:41