Gnu sed, will the p command append a newline when printing?












1















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



workflow chart



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?










share|improve this question









New contributor




Ogrish Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















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













  • 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
















1















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



workflow chart



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?










share|improve this question









New contributor




Ogrish Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















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













  • 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














1












1








1








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



workflow chart



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?










share|improve this question









New contributor




Ogrish Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












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



workflow chart



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






share|improve this question









New contributor




Ogrish Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Ogrish Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Jan 8 at 17:35









wjandrea

8,49742259




8,49742259






New contributor




Ogrish Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Jan 8 at 15:04









Ogrish ManOgrish Man

1626




1626




New contributor




Ogrish Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Ogrish Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Ogrish Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













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













  • 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













  • 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











  • 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

















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










1 Answer
1






active

oldest

votes


















1














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:



workflow chart



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:




  1. Read text 1<newline>, strip <newline> and put 1 in pattern space.


  2. p prints 1 and then prints a <newline>

  3. Read text 2<newline>, strip <newline> and put 2 in pattern space.


  4. p prints 2 and then prints a <newline>

  5. Read text 3<newline>, strip <newline> and put 3 in pattern space.


  6. p prints 3 and then prints a <newline>


For the second example:



# seq 3 | sed -rn 'x;p;x;p'

1

2

3


The process is:




  1. Read text 1<newline>, strip <newline> and put 1 in pattern space.

  2. Put 1 in hold space and put empty string in the pattern space.

  3. Print empty string and then print a <newline>.

  4. Put 1 back to the pattern space and put empty string in hold space.

  5. Print 1 and then print a <newline>

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






share|improve this answer










New contributor




Ogrish Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















    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.










    draft saved

    draft discarded


















    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









    1














    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:



    workflow chart



    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:




    1. Read text 1<newline>, strip <newline> and put 1 in pattern space.


    2. p prints 1 and then prints a <newline>

    3. Read text 2<newline>, strip <newline> and put 2 in pattern space.


    4. p prints 2 and then prints a <newline>

    5. Read text 3<newline>, strip <newline> and put 3 in pattern space.


    6. p prints 3 and then prints a <newline>


    For the second example:



    # seq 3 | sed -rn 'x;p;x;p'

    1

    2

    3


    The process is:




    1. Read text 1<newline>, strip <newline> and put 1 in pattern space.

    2. Put 1 in hold space and put empty string in the pattern space.

    3. Print empty string and then print a <newline>.

    4. Put 1 back to the pattern space and put empty string in hold space.

    5. Print 1 and then print a <newline>

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






    share|improve this answer










    New contributor




    Ogrish Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

























      1














      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:



      workflow chart



      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:




      1. Read text 1<newline>, strip <newline> and put 1 in pattern space.


      2. p prints 1 and then prints a <newline>

      3. Read text 2<newline>, strip <newline> and put 2 in pattern space.


      4. p prints 2 and then prints a <newline>

      5. Read text 3<newline>, strip <newline> and put 3 in pattern space.


      6. p prints 3 and then prints a <newline>


      For the second example:



      # seq 3 | sed -rn 'x;p;x;p'

      1

      2

      3


      The process is:




      1. Read text 1<newline>, strip <newline> and put 1 in pattern space.

      2. Put 1 in hold space and put empty string in the pattern space.

      3. Print empty string and then print a <newline>.

      4. Put 1 back to the pattern space and put empty string in hold space.

      5. Print 1 and then print a <newline>

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






      share|improve this answer










      New contributor




      Ogrish Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.























        1












        1








        1







        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:



        workflow chart



        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:




        1. Read text 1<newline>, strip <newline> and put 1 in pattern space.


        2. p prints 1 and then prints a <newline>

        3. Read text 2<newline>, strip <newline> and put 2 in pattern space.


        4. p prints 2 and then prints a <newline>

        5. Read text 3<newline>, strip <newline> and put 3 in pattern space.


        6. p prints 3 and then prints a <newline>


        For the second example:



        # seq 3 | sed -rn 'x;p;x;p'

        1

        2

        3


        The process is:




        1. Read text 1<newline>, strip <newline> and put 1 in pattern space.

        2. Put 1 in hold space and put empty string in the pattern space.

        3. Print empty string and then print a <newline>.

        4. Put 1 back to the pattern space and put empty string in hold space.

        5. Print 1 and then print a <newline>

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






        share|improve this answer










        New contributor




        Ogrish Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.










        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:



        workflow chart



        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:




        1. Read text 1<newline>, strip <newline> and put 1 in pattern space.


        2. p prints 1 and then prints a <newline>

        3. Read text 2<newline>, strip <newline> and put 2 in pattern space.


        4. p prints 2 and then prints a <newline>

        5. Read text 3<newline>, strip <newline> and put 3 in pattern space.


        6. p prints 3 and then prints a <newline>


        For the second example:



        # seq 3 | sed -rn 'x;p;x;p'

        1

        2

        3


        The process is:




        1. Read text 1<newline>, strip <newline> and put 1 in pattern space.

        2. Put 1 in hold space and put empty string in the pattern space.

        3. Print empty string and then print a <newline>.

        4. Put 1 back to the pattern space and put empty string in hold space.

        5. Print 1 and then print a <newline>

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







        share|improve this answer










        New contributor




        Ogrish Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer








        edited Jan 8 at 17:42









        wjandrea

        8,49742259




        8,49742259






        New contributor




        Ogrish Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered Jan 8 at 16:33









        Ogrish ManOgrish Man

        1626




        1626




        New contributor




        Ogrish Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        Ogrish Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        Ogrish Man is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






















            Ogrish Man is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            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.




            draft saved


            draft discarded














            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





















































            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 did Captain America manage to do this?

            迪纳利

            南乌拉尔铁路局