Gnu sed, will the p command append a newline when printing?
I'm using GNU sed 4.5, per sed --version
.
I'm new to sed and I created below sed's workflow based on my understanding (correct me if you find anything wrong).
So it seems the default auto printing of the pattern space will always include a newline at the end. My question is, will p
include a newline, too? I have below examples.
# seq 3 | sed -rn 'p'
1
2
3
Here the newline at the end of each number is added by sed itself (see the diagram "adds back newline to pattern space"). So it seems p
will not append a newline. However, see below example.
# seq 3 | sed -rn 'x;p;x;p'
1
2
3
Here x
exchange pattern space with hold space, which will result in an empty pattern space. Now p
applies to the pattern space (nothing in it) should print nothing. But based on the result, it seems here p
prints a newline. To me it seems this is inconsistent behavior. Can anyone help to explain, please?
sed
New contributor
|
show 3 more comments
I'm using GNU sed 4.5, per sed --version
.
I'm new to sed and I created below sed's workflow based on my understanding (correct me if you find anything wrong).
So it seems the default auto printing of the pattern space will always include a newline at the end. My question is, will p
include a newline, too? I have below examples.
# seq 3 | sed -rn 'p'
1
2
3
Here the newline at the end of each number is added by sed itself (see the diagram "adds back newline to pattern space"). So it seems p
will not append a newline. However, see below example.
# seq 3 | sed -rn 'x;p;x;p'
1
2
3
Here x
exchange pattern space with hold space, which will result in an empty pattern space. Now p
applies to the pattern space (nothing in it) should print nothing. But based on the result, it seems here p
prints a newline. To me it seems this is inconsistent behavior. Can anyone help to explain, please?
sed
New contributor
In the first example, the newline is not added by thep
command. It was added by sed itself. See sed's documentation at gnu.org/software/sed/manual/html_node/Execution-Cycle.html. It clearly states that: When the end of the script is reached, unless the -n option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed.
– Ogrish Man
Jan 8 at 15:34
But you are using the-n
option in the first example.
– PesaThe
Jan 8 at 15:40
As I understand, whether you are using-n
or not, sed will always add back a newline. With-n
, sed will not print the pattern space and you have to usep
to print it if necessary.
– Ogrish Man
Jan 8 at 15:44
The way I understand it is thatsed
prints the pattern space and adds the newline only if the-n
option wasn't used.
– PesaThe
Jan 8 at 15:47
Hmmm, interesting. I read that paragraph of the document several times and now I feel your understanding seems right. If so, that explains the behavior and I need to move "adds back newline to pattern space" under "sed has -n option" in the diagram.
– Ogrish Man
Jan 8 at 15:51
|
show 3 more comments
I'm using GNU sed 4.5, per sed --version
.
I'm new to sed and I created below sed's workflow based on my understanding (correct me if you find anything wrong).
So it seems the default auto printing of the pattern space will always include a newline at the end. My question is, will p
include a newline, too? I have below examples.
# seq 3 | sed -rn 'p'
1
2
3
Here the newline at the end of each number is added by sed itself (see the diagram "adds back newline to pattern space"). So it seems p
will not append a newline. However, see below example.
# seq 3 | sed -rn 'x;p;x;p'
1
2
3
Here x
exchange pattern space with hold space, which will result in an empty pattern space. Now p
applies to the pattern space (nothing in it) should print nothing. But based on the result, it seems here p
prints a newline. To me it seems this is inconsistent behavior. Can anyone help to explain, please?
sed
New contributor
I'm using GNU sed 4.5, per sed --version
.
I'm new to sed and I created below sed's workflow based on my understanding (correct me if you find anything wrong).
So it seems the default auto printing of the pattern space will always include a newline at the end. My question is, will p
include a newline, too? I have below examples.
# seq 3 | sed -rn 'p'
1
2
3
Here the newline at the end of each number is added by sed itself (see the diagram "adds back newline to pattern space"). So it seems p
will not append a newline. However, see below example.
# seq 3 | sed -rn 'x;p;x;p'
1
2
3
Here x
exchange pattern space with hold space, which will result in an empty pattern space. Now p
applies to the pattern space (nothing in it) should print nothing. But based on the result, it seems here p
prints a newline. To me it seems this is inconsistent behavior. Can anyone help to explain, please?
sed
sed
New contributor
New contributor
edited Jan 8 at 17:35
wjandrea
8,49742259
8,49742259
New contributor
asked Jan 8 at 15:04
Ogrish ManOgrish Man
1626
1626
New contributor
New contributor
In the first example, the newline is not added by thep
command. It was added by sed itself. See sed's documentation at gnu.org/software/sed/manual/html_node/Execution-Cycle.html. It clearly states that: When the end of the script is reached, unless the -n option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed.
– Ogrish Man
Jan 8 at 15:34
But you are using the-n
option in the first example.
– PesaThe
Jan 8 at 15:40
As I understand, whether you are using-n
or not, sed will always add back a newline. With-n
, sed will not print the pattern space and you have to usep
to print it if necessary.
– Ogrish Man
Jan 8 at 15:44
The way I understand it is thatsed
prints the pattern space and adds the newline only if the-n
option wasn't used.
– PesaThe
Jan 8 at 15:47
Hmmm, interesting. I read that paragraph of the document several times and now I feel your understanding seems right. If so, that explains the behavior and I need to move "adds back newline to pattern space" under "sed has -n option" in the diagram.
– Ogrish Man
Jan 8 at 15:51
|
show 3 more comments
In the first example, the newline is not added by thep
command. It was added by sed itself. See sed's documentation at gnu.org/software/sed/manual/html_node/Execution-Cycle.html. It clearly states that: When the end of the script is reached, unless the -n option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed.
– Ogrish Man
Jan 8 at 15:34
But you are using the-n
option in the first example.
– PesaThe
Jan 8 at 15:40
As I understand, whether you are using-n
or not, sed will always add back a newline. With-n
, sed will not print the pattern space and you have to usep
to print it if necessary.
– Ogrish Man
Jan 8 at 15:44
The way I understand it is thatsed
prints the pattern space and adds the newline only if the-n
option wasn't used.
– PesaThe
Jan 8 at 15:47
Hmmm, interesting. I read that paragraph of the document several times and now I feel your understanding seems right. If so, that explains the behavior and I need to move "adds back newline to pattern space" under "sed has -n option" in the diagram.
– Ogrish Man
Jan 8 at 15:51
In the first example, the newline is not added by the
p
command. It was added by sed itself. See sed's documentation at gnu.org/software/sed/manual/html_node/Execution-Cycle.html. It clearly states that: When the end of the script is reached, unless the -n option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed.– Ogrish Man
Jan 8 at 15:34
In the first example, the newline is not added by the
p
command. It was added by sed itself. See sed's documentation at gnu.org/software/sed/manual/html_node/Execution-Cycle.html. It clearly states that: When the end of the script is reached, unless the -n option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed.– Ogrish Man
Jan 8 at 15:34
But you are using the
-n
option in the first example.– PesaThe
Jan 8 at 15:40
But you are using the
-n
option in the first example.– PesaThe
Jan 8 at 15:40
As I understand, whether you are using
-n
or not, sed will always add back a newline. With -n
, sed will not print the pattern space and you have to use p
to print it if necessary.– Ogrish Man
Jan 8 at 15:44
As I understand, whether you are using
-n
or not, sed will always add back a newline. With -n
, sed will not print the pattern space and you have to use p
to print it if necessary.– Ogrish Man
Jan 8 at 15:44
The way I understand it is that
sed
prints the pattern space and adds the newline only if the -n
option wasn't used.– PesaThe
Jan 8 at 15:47
The way I understand it is that
sed
prints the pattern space and adds the newline only if the -n
option wasn't used.– PesaThe
Jan 8 at 15:47
Hmmm, interesting. I read that paragraph of the document several times and now I feel your understanding seems right. If so, that explains the behavior and I need to move "adds back newline to pattern space" under "sed has -n option" in the diagram.
– Ogrish Man
Jan 8 at 15:51
Hmmm, interesting. I read that paragraph of the document several times and now I feel your understanding seems right. If so, that explains the behavior and I need to move "adds back newline to pattern space" under "sed has -n option" in the diagram.
– Ogrish Man
Jan 8 at 15:51
|
show 3 more comments
1 Answer
1
active
oldest
votes
There is an error in the diagram. Adds back newline to pattern space only happens when sed is run without the -n
option. So the correct diagram should be:
Below is an excerpt from sed's documentation at How sed Works
When the end of the script is reached, unless the
-n
option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed. Then the next cycle starts for the next input line.
So if we run sed with -n
, there will be no Adds newline back step.
sed's documentation states that the p
command simply prints the pattern space. See Often-Used Commands for details.
p
Print out the pattern space (to the standard output). This command is usually only used in conjunction with the -n command-line option.
But it seems that p
will always append a newline at the end of the pattern space (please correct me if you think this is wrong).
Based on the above-mentioned facts, let take a look the examples.
# seq 3 | sed -rn 'p'
1
2
3
The process for this example is:
- Read text
1<newline>
, strip<newline>
and put1
in pattern space.
p
prints1
and then prints a<newline>
- Read text
2<newline>
, strip<newline>
and put2
in pattern space.
p
prints2
and then prints a<newline>
- Read text
3<newline>
, strip<newline>
and put3
in pattern space.
p
prints3
and then prints a<newline>
For the second example:
# seq 3 | sed -rn 'x;p;x;p'
1
2
3
The process is:
- Read text
1<newline>
, strip<newline>
and put1
in pattern space. - Put
1
in hold space and put empty string in the pattern space. - Print empty string and then print a
<newline>
. - Put
1
back to the pattern space and put empty string in hold space. - Print
1
and then print a<newline>
- Repeat step 1~5 for other input lines
So there is no inconsistency in these examples.
BTW, I'm still learning sed and this answer is based on my understanding. Please correct me if you find anything wrong. Thanks.
New contributor
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
});
}
});
Ogrish Man is a new contributor. Be nice, and check out our Code of Conduct.
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%2f1108018%2fgnu-sed-will-the-p-command-append-a-newline-when-printing%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
There is an error in the diagram. Adds back newline to pattern space only happens when sed is run without the -n
option. So the correct diagram should be:
Below is an excerpt from sed's documentation at How sed Works
When the end of the script is reached, unless the
-n
option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed. Then the next cycle starts for the next input line.
So if we run sed with -n
, there will be no Adds newline back step.
sed's documentation states that the p
command simply prints the pattern space. See Often-Used Commands for details.
p
Print out the pattern space (to the standard output). This command is usually only used in conjunction with the -n command-line option.
But it seems that p
will always append a newline at the end of the pattern space (please correct me if you think this is wrong).
Based on the above-mentioned facts, let take a look the examples.
# seq 3 | sed -rn 'p'
1
2
3
The process for this example is:
- Read text
1<newline>
, strip<newline>
and put1
in pattern space.
p
prints1
and then prints a<newline>
- Read text
2<newline>
, strip<newline>
and put2
in pattern space.
p
prints2
and then prints a<newline>
- Read text
3<newline>
, strip<newline>
and put3
in pattern space.
p
prints3
and then prints a<newline>
For the second example:
# seq 3 | sed -rn 'x;p;x;p'
1
2
3
The process is:
- Read text
1<newline>
, strip<newline>
and put1
in pattern space. - Put
1
in hold space and put empty string in the pattern space. - Print empty string and then print a
<newline>
. - Put
1
back to the pattern space and put empty string in hold space. - Print
1
and then print a<newline>
- Repeat step 1~5 for other input lines
So there is no inconsistency in these examples.
BTW, I'm still learning sed and this answer is based on my understanding. Please correct me if you find anything wrong. Thanks.
New contributor
add a comment |
There is an error in the diagram. Adds back newline to pattern space only happens when sed is run without the -n
option. So the correct diagram should be:
Below is an excerpt from sed's documentation at How sed Works
When the end of the script is reached, unless the
-n
option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed. Then the next cycle starts for the next input line.
So if we run sed with -n
, there will be no Adds newline back step.
sed's documentation states that the p
command simply prints the pattern space. See Often-Used Commands for details.
p
Print out the pattern space (to the standard output). This command is usually only used in conjunction with the -n command-line option.
But it seems that p
will always append a newline at the end of the pattern space (please correct me if you think this is wrong).
Based on the above-mentioned facts, let take a look the examples.
# seq 3 | sed -rn 'p'
1
2
3
The process for this example is:
- Read text
1<newline>
, strip<newline>
and put1
in pattern space.
p
prints1
and then prints a<newline>
- Read text
2<newline>
, strip<newline>
and put2
in pattern space.
p
prints2
and then prints a<newline>
- Read text
3<newline>
, strip<newline>
and put3
in pattern space.
p
prints3
and then prints a<newline>
For the second example:
# seq 3 | sed -rn 'x;p;x;p'
1
2
3
The process is:
- Read text
1<newline>
, strip<newline>
and put1
in pattern space. - Put
1
in hold space and put empty string in the pattern space. - Print empty string and then print a
<newline>
. - Put
1
back to the pattern space and put empty string in hold space. - Print
1
and then print a<newline>
- Repeat step 1~5 for other input lines
So there is no inconsistency in these examples.
BTW, I'm still learning sed and this answer is based on my understanding. Please correct me if you find anything wrong. Thanks.
New contributor
add a comment |
There is an error in the diagram. Adds back newline to pattern space only happens when sed is run without the -n
option. So the correct diagram should be:
Below is an excerpt from sed's documentation at How sed Works
When the end of the script is reached, unless the
-n
option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed. Then the next cycle starts for the next input line.
So if we run sed with -n
, there will be no Adds newline back step.
sed's documentation states that the p
command simply prints the pattern space. See Often-Used Commands for details.
p
Print out the pattern space (to the standard output). This command is usually only used in conjunction with the -n command-line option.
But it seems that p
will always append a newline at the end of the pattern space (please correct me if you think this is wrong).
Based on the above-mentioned facts, let take a look the examples.
# seq 3 | sed -rn 'p'
1
2
3
The process for this example is:
- Read text
1<newline>
, strip<newline>
and put1
in pattern space.
p
prints1
and then prints a<newline>
- Read text
2<newline>
, strip<newline>
and put2
in pattern space.
p
prints2
and then prints a<newline>
- Read text
3<newline>
, strip<newline>
and put3
in pattern space.
p
prints3
and then prints a<newline>
For the second example:
# seq 3 | sed -rn 'x;p;x;p'
1
2
3
The process is:
- Read text
1<newline>
, strip<newline>
and put1
in pattern space. - Put
1
in hold space and put empty string in the pattern space. - Print empty string and then print a
<newline>
. - Put
1
back to the pattern space and put empty string in hold space. - Print
1
and then print a<newline>
- Repeat step 1~5 for other input lines
So there is no inconsistency in these examples.
BTW, I'm still learning sed and this answer is based on my understanding. Please correct me if you find anything wrong. Thanks.
New contributor
There is an error in the diagram. Adds back newline to pattern space only happens when sed is run without the -n
option. So the correct diagram should be:
Below is an excerpt from sed's documentation at How sed Works
When the end of the script is reached, unless the
-n
option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed. Then the next cycle starts for the next input line.
So if we run sed with -n
, there will be no Adds newline back step.
sed's documentation states that the p
command simply prints the pattern space. See Often-Used Commands for details.
p
Print out the pattern space (to the standard output). This command is usually only used in conjunction with the -n command-line option.
But it seems that p
will always append a newline at the end of the pattern space (please correct me if you think this is wrong).
Based on the above-mentioned facts, let take a look the examples.
# seq 3 | sed -rn 'p'
1
2
3
The process for this example is:
- Read text
1<newline>
, strip<newline>
and put1
in pattern space.
p
prints1
and then prints a<newline>
- Read text
2<newline>
, strip<newline>
and put2
in pattern space.
p
prints2
and then prints a<newline>
- Read text
3<newline>
, strip<newline>
and put3
in pattern space.
p
prints3
and then prints a<newline>
For the second example:
# seq 3 | sed -rn 'x;p;x;p'
1
2
3
The process is:
- Read text
1<newline>
, strip<newline>
and put1
in pattern space. - Put
1
in hold space and put empty string in the pattern space. - Print empty string and then print a
<newline>
. - Put
1
back to the pattern space and put empty string in hold space. - Print
1
and then print a<newline>
- Repeat step 1~5 for other input lines
So there is no inconsistency in these examples.
BTW, I'm still learning sed and this answer is based on my understanding. Please correct me if you find anything wrong. Thanks.
New contributor
edited Jan 8 at 17:42
wjandrea
8,49742259
8,49742259
New contributor
answered Jan 8 at 16:33
Ogrish ManOgrish Man
1626
1626
New contributor
New contributor
add a comment |
add a comment |
Ogrish Man is a new contributor. Be nice, and check out our Code of Conduct.
Ogrish Man is a new contributor. Be nice, and check out our Code of Conduct.
Ogrish Man is a new contributor. Be nice, and check out our Code of Conduct.
Ogrish Man is a new contributor. Be nice, and check out our Code of Conduct.
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%2f1108018%2fgnu-sed-will-the-p-command-append-a-newline-when-printing%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
In the first example, the newline is not added by the
p
command. It was added by sed itself. See sed's documentation at gnu.org/software/sed/manual/html_node/Execution-Cycle.html. It clearly states that: When the end of the script is reached, unless the -n option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed.– Ogrish Man
Jan 8 at 15:34
But you are using the
-n
option in the first example.– PesaThe
Jan 8 at 15:40
As I understand, whether you are using
-n
or not, sed will always add back a newline. With-n
, sed will not print the pattern space and you have to usep
to print it if necessary.– Ogrish Man
Jan 8 at 15:44
The way I understand it is that
sed
prints the pattern space and adds the newline only if the-n
option wasn't used.– PesaThe
Jan 8 at 15:47
Hmmm, interesting. I read that paragraph of the document several times and now I feel your understanding seems right. If so, that explains the behavior and I need to move "adds back newline to pattern space" under "sed has -n option" in the diagram.
– Ogrish Man
Jan 8 at 15:51