Less ugly way to use sed to simply include a new line?
There are a lot of guides, handbooks, fast-guides, question/answers about it: no one are simple and objective...
It is a classical problem, near all text editors crashes with big files XML or HTML "all in one line", so we need to decide what tag will recive the n and replace all occurences of <tag by n<tag ... so simple. Why it is not simple to do by terminal?
The best question/answer about this case not solves: Bash: How can I replace a string by new line in osx bash?
Example using that solution: sed 's/<article/'$'nn<article/g' file.htm not works, need some more exotical syntax, so it is not simple as I solicitated in this question.
So, this quetion is not about "any solution", but about "some simple/elegant solution".
sed
|
show 1 more comment
There are a lot of guides, handbooks, fast-guides, question/answers about it: no one are simple and objective...
It is a classical problem, near all text editors crashes with big files XML or HTML "all in one line", so we need to decide what tag will recive the n and replace all occurences of <tag by n<tag ... so simple. Why it is not simple to do by terminal?
The best question/answer about this case not solves: Bash: How can I replace a string by new line in osx bash?
Example using that solution: sed 's/<article/'$'nn<article/g' file.htm not works, need some more exotical syntax, so it is not simple as I solicitated in this question.
So, this quetion is not about "any solution", but about "some simple/elegant solution".
sed
What have you tried? How did it fail?
– choroba
Nov 23 '18 at 13:53
perl -pe 's/(string)/n$1/' input
– William Pursell
Nov 23 '18 at 13:54
Hi @choroba, sorry, now including an example of sed command.
– Peter Krauss
Nov 23 '18 at 13:54
1
sedhas solutions, but there are many implementations of sed and not all permit the simple solution. Improving sed was one of the motivations for perl.
– William Pursell
Nov 23 '18 at 14:51
1
Your solutionsed 's/<article/'$'nn<article/g' file.htmis half right, a clearer way to view this is to replace'sed commands'by$'sed commands'. Thus all quoted characters (by which I mean character preceeded by a back slash) must also be quoted. Which is a long way of saying, try this:sed $'s/<article/\n\n<article/g' file.htm
– potong
Nov 23 '18 at 18:25
|
show 1 more comment
There are a lot of guides, handbooks, fast-guides, question/answers about it: no one are simple and objective...
It is a classical problem, near all text editors crashes with big files XML or HTML "all in one line", so we need to decide what tag will recive the n and replace all occurences of <tag by n<tag ... so simple. Why it is not simple to do by terminal?
The best question/answer about this case not solves: Bash: How can I replace a string by new line in osx bash?
Example using that solution: sed 's/<article/'$'nn<article/g' file.htm not works, need some more exotical syntax, so it is not simple as I solicitated in this question.
So, this quetion is not about "any solution", but about "some simple/elegant solution".
sed
There are a lot of guides, handbooks, fast-guides, question/answers about it: no one are simple and objective...
It is a classical problem, near all text editors crashes with big files XML or HTML "all in one line", so we need to decide what tag will recive the n and replace all occurences of <tag by n<tag ... so simple. Why it is not simple to do by terminal?
The best question/answer about this case not solves: Bash: How can I replace a string by new line in osx bash?
Example using that solution: sed 's/<article/'$'nn<article/g' file.htm not works, need some more exotical syntax, so it is not simple as I solicitated in this question.
So, this quetion is not about "any solution", but about "some simple/elegant solution".
sed
sed
edited Nov 23 '18 at 14:01
Peter Krauss
asked Nov 23 '18 at 13:50
Peter KraussPeter Krauss
5,3051081169
5,3051081169
What have you tried? How did it fail?
– choroba
Nov 23 '18 at 13:53
perl -pe 's/(string)/n$1/' input
– William Pursell
Nov 23 '18 at 13:54
Hi @choroba, sorry, now including an example of sed command.
– Peter Krauss
Nov 23 '18 at 13:54
1
sedhas solutions, but there are many implementations of sed and not all permit the simple solution. Improving sed was one of the motivations for perl.
– William Pursell
Nov 23 '18 at 14:51
1
Your solutionsed 's/<article/'$'nn<article/g' file.htmis half right, a clearer way to view this is to replace'sed commands'by$'sed commands'. Thus all quoted characters (by which I mean character preceeded by a back slash) must also be quoted. Which is a long way of saying, try this:sed $'s/<article/\n\n<article/g' file.htm
– potong
Nov 23 '18 at 18:25
|
show 1 more comment
What have you tried? How did it fail?
– choroba
Nov 23 '18 at 13:53
perl -pe 's/(string)/n$1/' input
– William Pursell
Nov 23 '18 at 13:54
Hi @choroba, sorry, now including an example of sed command.
– Peter Krauss
Nov 23 '18 at 13:54
1
sedhas solutions, but there are many implementations of sed and not all permit the simple solution. Improving sed was one of the motivations for perl.
– William Pursell
Nov 23 '18 at 14:51
1
Your solutionsed 's/<article/'$'nn<article/g' file.htmis half right, a clearer way to view this is to replace'sed commands'by$'sed commands'. Thus all quoted characters (by which I mean character preceeded by a back slash) must also be quoted. Which is a long way of saying, try this:sed $'s/<article/\n\n<article/g' file.htm
– potong
Nov 23 '18 at 18:25
What have you tried? How did it fail?
– choroba
Nov 23 '18 at 13:53
What have you tried? How did it fail?
– choroba
Nov 23 '18 at 13:53
perl -pe 's/(string)/n$1/' input– William Pursell
Nov 23 '18 at 13:54
perl -pe 's/(string)/n$1/' input– William Pursell
Nov 23 '18 at 13:54
Hi @choroba, sorry, now including an example of sed command.
– Peter Krauss
Nov 23 '18 at 13:54
Hi @choroba, sorry, now including an example of sed command.
– Peter Krauss
Nov 23 '18 at 13:54
1
1
sed has solutions, but there are many implementations of sed and not all permit the simple solution. Improving sed was one of the motivations for perl.– William Pursell
Nov 23 '18 at 14:51
sed has solutions, but there are many implementations of sed and not all permit the simple solution. Improving sed was one of the motivations for perl.– William Pursell
Nov 23 '18 at 14:51
1
1
Your solution
sed 's/<article/'$'nn<article/g' file.htm is half right, a clearer way to view this is to replace 'sed commands' by $'sed commands'. Thus all quoted characters (by which I mean character preceeded by a back slash) must also be quoted. Which is a long way of saying, try this: sed $'s/<article/\n\n<article/g' file.htm– potong
Nov 23 '18 at 18:25
Your solution
sed 's/<article/'$'nn<article/g' file.htm is half right, a clearer way to view this is to replace 'sed commands' by $'sed commands'. Thus all quoted characters (by which I mean character preceeded by a back slash) must also be quoted. Which is a long way of saying, try this: sed $'s/<article/\n\n<article/g' file.htm– potong
Nov 23 '18 at 18:25
|
show 1 more comment
1 Answer
1
active
oldest
votes
If I understand what you are looking for you could try something like the following:
sed 's/<tag>/n<tag>/g' file.htm
which is very close to the anwser you linked.
It already looks quite simple to me, it replaces the tag with a new line character and writes the tag again.
However I don't get the need for this '$' in your case.
YES! My first use wassed '/<tag/nn<tag/g' filethat generated an error and my looking for guides... Why sometimes we needs/and sometimes not withsed?
– Peter Krauss
Nov 23 '18 at 14:04
sstands for substitution. From the sed man page:s/regexp/replacement/ Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. ...
– matteo martelli
Nov 23 '18 at 14:06
@PeterKrauss, please consider to accept the answer if you think it's correct and suits your issue. Thanks
– matteo martelli
Nov 23 '18 at 14:11
2
Patterns without the leadingsare searches to decide which lines to act on.s///is an action. sosed '/foo/ s/bar/bye/g'means "only replace bar with bye on lines that have a foo." GNU has a great manual here.
– Paul Hodges
Nov 23 '18 at 14:17
1
Be careful: this solution does not work in all implementations of sed.
– William Pursell
Nov 23 '18 at 14:50
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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%2fstackoverflow.com%2fquestions%2f53447946%2fless-ugly-way-to-use-sed-to-simply-include-a-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 understand what you are looking for you could try something like the following:
sed 's/<tag>/n<tag>/g' file.htm
which is very close to the anwser you linked.
It already looks quite simple to me, it replaces the tag with a new line character and writes the tag again.
However I don't get the need for this '$' in your case.
YES! My first use wassed '/<tag/nn<tag/g' filethat generated an error and my looking for guides... Why sometimes we needs/and sometimes not withsed?
– Peter Krauss
Nov 23 '18 at 14:04
sstands for substitution. From the sed man page:s/regexp/replacement/ Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. ...
– matteo martelli
Nov 23 '18 at 14:06
@PeterKrauss, please consider to accept the answer if you think it's correct and suits your issue. Thanks
– matteo martelli
Nov 23 '18 at 14:11
2
Patterns without the leadingsare searches to decide which lines to act on.s///is an action. sosed '/foo/ s/bar/bye/g'means "only replace bar with bye on lines that have a foo." GNU has a great manual here.
– Paul Hodges
Nov 23 '18 at 14:17
1
Be careful: this solution does not work in all implementations of sed.
– William Pursell
Nov 23 '18 at 14:50
|
show 1 more comment
If I understand what you are looking for you could try something like the following:
sed 's/<tag>/n<tag>/g' file.htm
which is very close to the anwser you linked.
It already looks quite simple to me, it replaces the tag with a new line character and writes the tag again.
However I don't get the need for this '$' in your case.
YES! My first use wassed '/<tag/nn<tag/g' filethat generated an error and my looking for guides... Why sometimes we needs/and sometimes not withsed?
– Peter Krauss
Nov 23 '18 at 14:04
sstands for substitution. From the sed man page:s/regexp/replacement/ Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. ...
– matteo martelli
Nov 23 '18 at 14:06
@PeterKrauss, please consider to accept the answer if you think it's correct and suits your issue. Thanks
– matteo martelli
Nov 23 '18 at 14:11
2
Patterns without the leadingsare searches to decide which lines to act on.s///is an action. sosed '/foo/ s/bar/bye/g'means "only replace bar with bye on lines that have a foo." GNU has a great manual here.
– Paul Hodges
Nov 23 '18 at 14:17
1
Be careful: this solution does not work in all implementations of sed.
– William Pursell
Nov 23 '18 at 14:50
|
show 1 more comment
If I understand what you are looking for you could try something like the following:
sed 's/<tag>/n<tag>/g' file.htm
which is very close to the anwser you linked.
It already looks quite simple to me, it replaces the tag with a new line character and writes the tag again.
However I don't get the need for this '$' in your case.
If I understand what you are looking for you could try something like the following:
sed 's/<tag>/n<tag>/g' file.htm
which is very close to the anwser you linked.
It already looks quite simple to me, it replaces the tag with a new line character and writes the tag again.
However I don't get the need for this '$' in your case.
edited Nov 23 '18 at 14:04
answered Nov 23 '18 at 14:00
matteo martellimatteo martelli
764
764
YES! My first use wassed '/<tag/nn<tag/g' filethat generated an error and my looking for guides... Why sometimes we needs/and sometimes not withsed?
– Peter Krauss
Nov 23 '18 at 14:04
sstands for substitution. From the sed man page:s/regexp/replacement/ Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. ...
– matteo martelli
Nov 23 '18 at 14:06
@PeterKrauss, please consider to accept the answer if you think it's correct and suits your issue. Thanks
– matteo martelli
Nov 23 '18 at 14:11
2
Patterns without the leadingsare searches to decide which lines to act on.s///is an action. sosed '/foo/ s/bar/bye/g'means "only replace bar with bye on lines that have a foo." GNU has a great manual here.
– Paul Hodges
Nov 23 '18 at 14:17
1
Be careful: this solution does not work in all implementations of sed.
– William Pursell
Nov 23 '18 at 14:50
|
show 1 more comment
YES! My first use wassed '/<tag/nn<tag/g' filethat generated an error and my looking for guides... Why sometimes we needs/and sometimes not withsed?
– Peter Krauss
Nov 23 '18 at 14:04
sstands for substitution. From the sed man page:s/regexp/replacement/ Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. ...
– matteo martelli
Nov 23 '18 at 14:06
@PeterKrauss, please consider to accept the answer if you think it's correct and suits your issue. Thanks
– matteo martelli
Nov 23 '18 at 14:11
2
Patterns without the leadingsare searches to decide which lines to act on.s///is an action. sosed '/foo/ s/bar/bye/g'means "only replace bar with bye on lines that have a foo." GNU has a great manual here.
– Paul Hodges
Nov 23 '18 at 14:17
1
Be careful: this solution does not work in all implementations of sed.
– William Pursell
Nov 23 '18 at 14:50
YES! My first use was
sed '/<tag/nn<tag/g' file that generated an error and my looking for guides... Why sometimes we need s/ and sometimes not with sed ?– Peter Krauss
Nov 23 '18 at 14:04
YES! My first use was
sed '/<tag/nn<tag/g' file that generated an error and my looking for guides... Why sometimes we need s/ and sometimes not with sed ?– Peter Krauss
Nov 23 '18 at 14:04
s stands for substitution. From the sed man page: s/regexp/replacement/ Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. ...– matteo martelli
Nov 23 '18 at 14:06
s stands for substitution. From the sed man page: s/regexp/replacement/ Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. ...– matteo martelli
Nov 23 '18 at 14:06
@PeterKrauss, please consider to accept the answer if you think it's correct and suits your issue. Thanks
– matteo martelli
Nov 23 '18 at 14:11
@PeterKrauss, please consider to accept the answer if you think it's correct and suits your issue. Thanks
– matteo martelli
Nov 23 '18 at 14:11
2
2
Patterns without the leading
s are searches to decide which lines to act on. s/// is an action. so sed '/foo/ s/bar/bye/g' means "only replace bar with bye on lines that have a foo." GNU has a great manual here.– Paul Hodges
Nov 23 '18 at 14:17
Patterns without the leading
s are searches to decide which lines to act on. s/// is an action. so sed '/foo/ s/bar/bye/g' means "only replace bar with bye on lines that have a foo." GNU has a great manual here.– Paul Hodges
Nov 23 '18 at 14:17
1
1
Be careful: this solution does not work in all implementations of sed.
– William Pursell
Nov 23 '18 at 14:50
Be careful: this solution does not work in all implementations of sed.
– William Pursell
Nov 23 '18 at 14:50
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53447946%2fless-ugly-way-to-use-sed-to-simply-include-a-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
What have you tried? How did it fail?
– choroba
Nov 23 '18 at 13:53
perl -pe 's/(string)/n$1/' input– William Pursell
Nov 23 '18 at 13:54
Hi @choroba, sorry, now including an example of sed command.
– Peter Krauss
Nov 23 '18 at 13:54
1
sedhas solutions, but there are many implementations of sed and not all permit the simple solution. Improving sed was one of the motivations for perl.– William Pursell
Nov 23 '18 at 14:51
1
Your solution
sed 's/<article/'$'nn<article/g' file.htmis half right, a clearer way to view this is to replace'sed commands'by$'sed commands'. Thus all quoted characters (by which I mean character preceeded by a back slash) must also be quoted. Which is a long way of saying, try this:sed $'s/<article/\n\n<article/g' file.htm– potong
Nov 23 '18 at 18:25