Replace using SED + Vars + New Line












0















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









share|improve this question

























  • :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
















0















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









share|improve this question

























  • :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














0












0








0








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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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

















: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










1 Answer
1






active

oldest

votes


















1














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






share|improve this answer
























  • It’s (IMHO) simpler to put the string in quotes; i.e., c='aw1naw2naw3' or c="aw1naw2naw3".

    – Scott
    Mar 1 at 0:15












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
});


}
});














draft saved

draft discarded


















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









1














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






share|improve this answer
























  • It’s (IMHO) simpler to put the string in quotes; i.e., c='aw1naw2naw3' or c="aw1naw2naw3".

    – Scott
    Mar 1 at 0:15
















1














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






share|improve this answer
























  • It’s (IMHO) simpler to put the string in quotes; i.e., c='aw1naw2naw3' or c="aw1naw2naw3".

    – Scott
    Mar 1 at 0:15














1












1








1







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 28 at 16:08









JuanJuan

1194




1194













  • 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

















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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How do I know what Microsoft account the skydrive app is syncing to?

When does type information flow backwards in C++?

Grease: Live!