does sed treat newline and regular character identically?
Given a data file tmp_file,
5 0 0 0
0 5 0 0
0 0 6 0
0 0 0 6
the following two commands render different results, why is that?
sed 's/n/ /g' tmp_file
5 0 0 0
0 5 0 0
0 0 6 0
0 0 0 6
sed 's/0/ /g' tmp_file
5
5
6
6
EDIT:
a previous post, How can I replace a newline (n) using sed?, was suggested to have resolved the issue. Surely the solution is the same, but the question looks seemingly different in the eye of a newbie. Also, one should not expect anybody is able to find the right answer among millions of posts even with hours of research online as done in the current post. I would rather withdraw the post if a negative mark is given due to being similar to previous posts.
search sed replace newline
add a comment |
Given a data file tmp_file,
5 0 0 0
0 5 0 0
0 0 6 0
0 0 0 6
the following two commands render different results, why is that?
sed 's/n/ /g' tmp_file
5 0 0 0
0 5 0 0
0 0 6 0
0 0 0 6
sed 's/0/ /g' tmp_file
5
5
6
6
EDIT:
a previous post, How can I replace a newline (n) using sed?, was suggested to have resolved the issue. Surely the solution is the same, but the question looks seemingly different in the eye of a newbie. Also, one should not expect anybody is able to find the right answer among millions of posts even with hours of research online as done in the current post. I would rather withdraw the post if a negative mark is given due to being similar to previous posts.
search sed replace newline
Possible duplicate of How can I replace a newline (n) using sed?
– Wiktor Stribiżew
Nov 18 '18 at 11:59
See here for how the sed cycle is defined.
– potong
Nov 19 '18 at 11:45
add a comment |
Given a data file tmp_file,
5 0 0 0
0 5 0 0
0 0 6 0
0 0 0 6
the following two commands render different results, why is that?
sed 's/n/ /g' tmp_file
5 0 0 0
0 5 0 0
0 0 6 0
0 0 0 6
sed 's/0/ /g' tmp_file
5
5
6
6
EDIT:
a previous post, How can I replace a newline (n) using sed?, was suggested to have resolved the issue. Surely the solution is the same, but the question looks seemingly different in the eye of a newbie. Also, one should not expect anybody is able to find the right answer among millions of posts even with hours of research online as done in the current post. I would rather withdraw the post if a negative mark is given due to being similar to previous posts.
search sed replace newline
Given a data file tmp_file,
5 0 0 0
0 5 0 0
0 0 6 0
0 0 0 6
the following two commands render different results, why is that?
sed 's/n/ /g' tmp_file
5 0 0 0
0 5 0 0
0 0 6 0
0 0 0 6
sed 's/0/ /g' tmp_file
5
5
6
6
EDIT:
a previous post, How can I replace a newline (n) using sed?, was suggested to have resolved the issue. Surely the solution is the same, but the question looks seemingly different in the eye of a newbie. Also, one should not expect anybody is able to find the right answer among millions of posts even with hours of research online as done in the current post. I would rather withdraw the post if a negative mark is given due to being similar to previous posts.
search sed replace newline
search sed replace newline
edited Nov 18 '18 at 22:21
bsmile
asked Nov 18 '18 at 2:41
bsmilebsmile
156
156
Possible duplicate of How can I replace a newline (n) using sed?
– Wiktor Stribiżew
Nov 18 '18 at 11:59
See here for how the sed cycle is defined.
– potong
Nov 19 '18 at 11:45
add a comment |
Possible duplicate of How can I replace a newline (n) using sed?
– Wiktor Stribiżew
Nov 18 '18 at 11:59
See here for how the sed cycle is defined.
– potong
Nov 19 '18 at 11:45
Possible duplicate of How can I replace a newline (n) using sed?
– Wiktor Stribiżew
Nov 18 '18 at 11:59
Possible duplicate of How can I replace a newline (n) using sed?
– Wiktor Stribiżew
Nov 18 '18 at 11:59
See here for how the sed cycle is defined.
– potong
Nov 19 '18 at 11:45
See here for how the sed cycle is defined.
– potong
Nov 19 '18 at 11:45
add a comment |
1 Answer
1
active
oldest
votes
Let us try to understand how sed
reads the Input_file, so sed
reads the lines by separator which is new line it self. Which means if lines are separated with new line a single line CANNOT have new line in it, until/unless we do let sed
know to read whole Input_file in a loop and then replace the new lines(kind of keeping values in hold space which is out of box functionality provided by sed
to keep values save in memory for operational purposes). So that is why your first command is NOT having any affect since sed
not found any line which is having new line in it.
Here is very nice thread(How can I replace a newline (n) using sed? ) I found over SO you could go through its solution and its explanation(taken from that thread) as follows.
sed ':a;N;$!ba;s/n/ /g' Input_file
Explanation of above code:
1-Create a label via :a.
2- Append the current and next line to the pattern space via N.
3- If we are before the last line, branch to the created label $!ba ($! means not to do it on the last line as there should be one final newline).
4- Finally the substitution replaces every newline with a space on the pattern space (which is the whole file).
I wanted to add my explanation above and then add that thread's details.
Thank you very much for the explanation. Now I get it! So indeed n and other character(regular or special) are different. Basically, you said that I need to read in the whole input file in order to do the replacement of n with space. BTW, N might refer to only append the next line to the pattern buffer instead of including the current line as well, or otherwise, every time the process branches, the current line would be read in twice. Is it?
– bsmile
Nov 18 '18 at 3:30
add a 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%2f53357418%2fdoes-sed-treat-newline-and-regular-character-identically%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
Let us try to understand how sed
reads the Input_file, so sed
reads the lines by separator which is new line it self. Which means if lines are separated with new line a single line CANNOT have new line in it, until/unless we do let sed
know to read whole Input_file in a loop and then replace the new lines(kind of keeping values in hold space which is out of box functionality provided by sed
to keep values save in memory for operational purposes). So that is why your first command is NOT having any affect since sed
not found any line which is having new line in it.
Here is very nice thread(How can I replace a newline (n) using sed? ) I found over SO you could go through its solution and its explanation(taken from that thread) as follows.
sed ':a;N;$!ba;s/n/ /g' Input_file
Explanation of above code:
1-Create a label via :a.
2- Append the current and next line to the pattern space via N.
3- If we are before the last line, branch to the created label $!ba ($! means not to do it on the last line as there should be one final newline).
4- Finally the substitution replaces every newline with a space on the pattern space (which is the whole file).
I wanted to add my explanation above and then add that thread's details.
Thank you very much for the explanation. Now I get it! So indeed n and other character(regular or special) are different. Basically, you said that I need to read in the whole input file in order to do the replacement of n with space. BTW, N might refer to only append the next line to the pattern buffer instead of including the current line as well, or otherwise, every time the process branches, the current line would be read in twice. Is it?
– bsmile
Nov 18 '18 at 3:30
add a comment |
Let us try to understand how sed
reads the Input_file, so sed
reads the lines by separator which is new line it self. Which means if lines are separated with new line a single line CANNOT have new line in it, until/unless we do let sed
know to read whole Input_file in a loop and then replace the new lines(kind of keeping values in hold space which is out of box functionality provided by sed
to keep values save in memory for operational purposes). So that is why your first command is NOT having any affect since sed
not found any line which is having new line in it.
Here is very nice thread(How can I replace a newline (n) using sed? ) I found over SO you could go through its solution and its explanation(taken from that thread) as follows.
sed ':a;N;$!ba;s/n/ /g' Input_file
Explanation of above code:
1-Create a label via :a.
2- Append the current and next line to the pattern space via N.
3- If we are before the last line, branch to the created label $!ba ($! means not to do it on the last line as there should be one final newline).
4- Finally the substitution replaces every newline with a space on the pattern space (which is the whole file).
I wanted to add my explanation above and then add that thread's details.
Thank you very much for the explanation. Now I get it! So indeed n and other character(regular or special) are different. Basically, you said that I need to read in the whole input file in order to do the replacement of n with space. BTW, N might refer to only append the next line to the pattern buffer instead of including the current line as well, or otherwise, every time the process branches, the current line would be read in twice. Is it?
– bsmile
Nov 18 '18 at 3:30
add a comment |
Let us try to understand how sed
reads the Input_file, so sed
reads the lines by separator which is new line it self. Which means if lines are separated with new line a single line CANNOT have new line in it, until/unless we do let sed
know to read whole Input_file in a loop and then replace the new lines(kind of keeping values in hold space which is out of box functionality provided by sed
to keep values save in memory for operational purposes). So that is why your first command is NOT having any affect since sed
not found any line which is having new line in it.
Here is very nice thread(How can I replace a newline (n) using sed? ) I found over SO you could go through its solution and its explanation(taken from that thread) as follows.
sed ':a;N;$!ba;s/n/ /g' Input_file
Explanation of above code:
1-Create a label via :a.
2- Append the current and next line to the pattern space via N.
3- If we are before the last line, branch to the created label $!ba ($! means not to do it on the last line as there should be one final newline).
4- Finally the substitution replaces every newline with a space on the pattern space (which is the whole file).
I wanted to add my explanation above and then add that thread's details.
Let us try to understand how sed
reads the Input_file, so sed
reads the lines by separator which is new line it self. Which means if lines are separated with new line a single line CANNOT have new line in it, until/unless we do let sed
know to read whole Input_file in a loop and then replace the new lines(kind of keeping values in hold space which is out of box functionality provided by sed
to keep values save in memory for operational purposes). So that is why your first command is NOT having any affect since sed
not found any line which is having new line in it.
Here is very nice thread(How can I replace a newline (n) using sed? ) I found over SO you could go through its solution and its explanation(taken from that thread) as follows.
sed ':a;N;$!ba;s/n/ /g' Input_file
Explanation of above code:
1-Create a label via :a.
2- Append the current and next line to the pattern space via N.
3- If we are before the last line, branch to the created label $!ba ($! means not to do it on the last line as there should be one final newline).
4- Finally the substitution replaces every newline with a space on the pattern space (which is the whole file).
I wanted to add my explanation above and then add that thread's details.
edited Nov 18 '18 at 3:07
answered Nov 18 '18 at 3:00
RavinderSingh13RavinderSingh13
26.5k41438
26.5k41438
Thank you very much for the explanation. Now I get it! So indeed n and other character(regular or special) are different. Basically, you said that I need to read in the whole input file in order to do the replacement of n with space. BTW, N might refer to only append the next line to the pattern buffer instead of including the current line as well, or otherwise, every time the process branches, the current line would be read in twice. Is it?
– bsmile
Nov 18 '18 at 3:30
add a comment |
Thank you very much for the explanation. Now I get it! So indeed n and other character(regular or special) are different. Basically, you said that I need to read in the whole input file in order to do the replacement of n with space. BTW, N might refer to only append the next line to the pattern buffer instead of including the current line as well, or otherwise, every time the process branches, the current line would be read in twice. Is it?
– bsmile
Nov 18 '18 at 3:30
Thank you very much for the explanation. Now I get it! So indeed n and other character(regular or special) are different. Basically, you said that I need to read in the whole input file in order to do the replacement of n with space. BTW, N might refer to only append the next line to the pattern buffer instead of including the current line as well, or otherwise, every time the process branches, the current line would be read in twice. Is it?
– bsmile
Nov 18 '18 at 3:30
Thank you very much for the explanation. Now I get it! So indeed n and other character(regular or special) are different. Basically, you said that I need to read in the whole input file in order to do the replacement of n with space. BTW, N might refer to only append the next line to the pattern buffer instead of including the current line as well, or otherwise, every time the process branches, the current line would be read in twice. Is it?
– bsmile
Nov 18 '18 at 3:30
add a 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%2f53357418%2fdoes-sed-treat-newline-and-regular-character-identically%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
Possible duplicate of How can I replace a newline (n) using sed?
– Wiktor Stribiżew
Nov 18 '18 at 11:59
See here for how the sed cycle is defined.
– potong
Nov 19 '18 at 11:45