Delete empty line if there is only one between two lines, if there are two empty lines remove only one












1














Hi Team I am getting db2 output as of below.



this    is    testing  1

this is testing 2


this is testing 4


The db2 provides output as, output for a query and a empty row and if there is no data for the query it provides one empty row



I want to remove the additional empty line after each output.



I know sed -i '/^$/d' file.txt can remove empty rows.
Is there a way to remove single empty row after each row which has data.



This is the desired output:



this    is    testing  1
this is testing 2

this is testing 4









share|improve this question









New contributor




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

























    1














    Hi Team I am getting db2 output as of below.



    this    is    testing  1

    this is testing 2


    this is testing 4


    The db2 provides output as, output for a query and a empty row and if there is no data for the query it provides one empty row



    I want to remove the additional empty line after each output.



    I know sed -i '/^$/d' file.txt can remove empty rows.
    Is there a way to remove single empty row after each row which has data.



    This is the desired output:



    this    is    testing  1
    this is testing 2

    this is testing 4









    share|improve this question









    New contributor




    Anthony 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







      Hi Team I am getting db2 output as of below.



      this    is    testing  1

      this is testing 2


      this is testing 4


      The db2 provides output as, output for a query and a empty row and if there is no data for the query it provides one empty row



      I want to remove the additional empty line after each output.



      I know sed -i '/^$/d' file.txt can remove empty rows.
      Is there a way to remove single empty row after each row which has data.



      This is the desired output:



      this    is    testing  1
      this is testing 2

      this is testing 4









      share|improve this question









      New contributor




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











      Hi Team I am getting db2 output as of below.



      this    is    testing  1

      this is testing 2


      this is testing 4


      The db2 provides output as, output for a query and a empty row and if there is no data for the query it provides one empty row



      I want to remove the additional empty line after each output.



      I know sed -i '/^$/d' file.txt can remove empty rows.
      Is there a way to remove single empty row after each row which has data.



      This is the desired output:



      this    is    testing  1
      this is testing 2

      this is testing 4






      shell-script






      share|improve this question









      New contributor




      Anthony 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




      Anthony 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 18 hours ago









      cryptarch

      5116




      5116






      New contributor




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









      asked 19 hours ago









      Anthony

      61




      61




      New contributor




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





      New contributor





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






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






















          5 Answers
          5






          active

          oldest

          votes


















          2














          This will only remove a newline if after some character (only the first newline after a line with text):



           sed -n '$!N;s/(.)n/1/;P;D' file





          share|improve this answer





























            1














            $ awk '{ print; if (length) getline }' file
            this is testing 1
            this is testing 2

            this is testing 4


            This prints the result and reads past the empty line after it if the result had any contents. For empty results, no additional line is skipped.



            This would print multiple consecutive empty lines only if there was multiple consecutive empty results. The output would have exactly one line per result (empty or not):



            $ cat file
            this is testing 1

            this is testing 2



            this is testing 5

            $ awk '{ print; if (length) getline }' file
            this is testing 1
            this is testing 2


            this is testing 5





            share|improve this answer































              0














              awk 'BEGIN {prev_empty = "TRUE"}; {if (/^$/) {if (prev_empty) {print}; prev_empty = "TRUE"} else {prev_empty = ""; print}}' file.txt


              Explanation





              • BEGIN {prev_empty = "TRUE"}: start by initialising the variable prev_empty to TRUE.


              • if (/^$/): check if the line is empty (i.e. matches the regex /^$/).

              • If so, then {if (prev_empty == "TRUE") {print}; prev_empty = "TRUE"}: if the previous line were empty, then print it, otherwise don't print (i.e. delete it). Then set the variable prev_empty to TRUE.

              • If the current line is not empty, {prev_empty = ""; print}: set the variable prev_empty to nothing, and print the line.


              The logic is that in almost all situations, print the current line. However, don't print the current line if the current line is empty (matches /^$/), and the previous line was non-empty (i.e. prev_empty = "").






              share|improve this answer































                0














                If the file is small enough to fit in memory, you can slurp the file with perl and remove any newline that comes after a non-space character and another newline:



                $ perl -0pe 's/(Sn)n/$1/g' file 
                this is testing 1
                this is testing 2

                this is testing 4





                share|improve this answer





























                  0














                  With Awk, you could set the record separator to double-newline and then print each record with the default single newline output record separator.



                  With POSIX Awk, that will result in an extra newline after the last record - if you have GNU Awk (gawk) you can prevent that by setting an empty output record separator if the actual input record separator is empty:



                  $ gawk -vRS='nn' 'RT=="" {ORS=""} 1' file.txt
                  this is testing 1
                  this is testing 2

                  this is testing 4





                  share|improve this answer





















                    Your Answer








                    StackExchange.ready(function() {
                    var channelOptions = {
                    tags: "".split(" "),
                    id: "106"
                    };
                    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: false,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: null,
                    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
                    });


                    }
                    });






                    Anthony 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%2funix.stackexchange.com%2fquestions%2f492410%2fdelete-empty-line-if-there-is-only-one-between-two-lines-if-there-are-two-empty%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    5 Answers
                    5






                    active

                    oldest

                    votes








                    5 Answers
                    5






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    2














                    This will only remove a newline if after some character (only the first newline after a line with text):



                     sed -n '$!N;s/(.)n/1/;P;D' file





                    share|improve this answer


























                      2














                      This will only remove a newline if after some character (only the first newline after a line with text):



                       sed -n '$!N;s/(.)n/1/;P;D' file





                      share|improve this answer
























                        2












                        2








                        2






                        This will only remove a newline if after some character (only the first newline after a line with text):



                         sed -n '$!N;s/(.)n/1/;P;D' file





                        share|improve this answer












                        This will only remove a newline if after some character (only the first newline after a line with text):



                         sed -n '$!N;s/(.)n/1/;P;D' file






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered 18 hours ago









                        Isaac

                        11.4k11650




                        11.4k11650

























                            1














                            $ awk '{ print; if (length) getline }' file
                            this is testing 1
                            this is testing 2

                            this is testing 4


                            This prints the result and reads past the empty line after it if the result had any contents. For empty results, no additional line is skipped.



                            This would print multiple consecutive empty lines only if there was multiple consecutive empty results. The output would have exactly one line per result (empty or not):



                            $ cat file
                            this is testing 1

                            this is testing 2



                            this is testing 5

                            $ awk '{ print; if (length) getline }' file
                            this is testing 1
                            this is testing 2


                            this is testing 5





                            share|improve this answer




























                              1














                              $ awk '{ print; if (length) getline }' file
                              this is testing 1
                              this is testing 2

                              this is testing 4


                              This prints the result and reads past the empty line after it if the result had any contents. For empty results, no additional line is skipped.



                              This would print multiple consecutive empty lines only if there was multiple consecutive empty results. The output would have exactly one line per result (empty or not):



                              $ cat file
                              this is testing 1

                              this is testing 2



                              this is testing 5

                              $ awk '{ print; if (length) getline }' file
                              this is testing 1
                              this is testing 2


                              this is testing 5





                              share|improve this answer


























                                1












                                1








                                1






                                $ awk '{ print; if (length) getline }' file
                                this is testing 1
                                this is testing 2

                                this is testing 4


                                This prints the result and reads past the empty line after it if the result had any contents. For empty results, no additional line is skipped.



                                This would print multiple consecutive empty lines only if there was multiple consecutive empty results. The output would have exactly one line per result (empty or not):



                                $ cat file
                                this is testing 1

                                this is testing 2



                                this is testing 5

                                $ awk '{ print; if (length) getline }' file
                                this is testing 1
                                this is testing 2


                                this is testing 5





                                share|improve this answer














                                $ awk '{ print; if (length) getline }' file
                                this is testing 1
                                this is testing 2

                                this is testing 4


                                This prints the result and reads past the empty line after it if the result had any contents. For empty results, no additional line is skipped.



                                This would print multiple consecutive empty lines only if there was multiple consecutive empty results. The output would have exactly one line per result (empty or not):



                                $ cat file
                                this is testing 1

                                this is testing 2



                                this is testing 5

                                $ awk '{ print; if (length) getline }' file
                                this is testing 1
                                this is testing 2


                                this is testing 5






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited 17 hours ago

























                                answered 18 hours ago









                                Kusalananda

                                122k16230375




                                122k16230375























                                    0














                                    awk 'BEGIN {prev_empty = "TRUE"}; {if (/^$/) {if (prev_empty) {print}; prev_empty = "TRUE"} else {prev_empty = ""; print}}' file.txt


                                    Explanation





                                    • BEGIN {prev_empty = "TRUE"}: start by initialising the variable prev_empty to TRUE.


                                    • if (/^$/): check if the line is empty (i.e. matches the regex /^$/).

                                    • If so, then {if (prev_empty == "TRUE") {print}; prev_empty = "TRUE"}: if the previous line were empty, then print it, otherwise don't print (i.e. delete it). Then set the variable prev_empty to TRUE.

                                    • If the current line is not empty, {prev_empty = ""; print}: set the variable prev_empty to nothing, and print the line.


                                    The logic is that in almost all situations, print the current line. However, don't print the current line if the current line is empty (matches /^$/), and the previous line was non-empty (i.e. prev_empty = "").






                                    share|improve this answer




























                                      0














                                      awk 'BEGIN {prev_empty = "TRUE"}; {if (/^$/) {if (prev_empty) {print}; prev_empty = "TRUE"} else {prev_empty = ""; print}}' file.txt


                                      Explanation





                                      • BEGIN {prev_empty = "TRUE"}: start by initialising the variable prev_empty to TRUE.


                                      • if (/^$/): check if the line is empty (i.e. matches the regex /^$/).

                                      • If so, then {if (prev_empty == "TRUE") {print}; prev_empty = "TRUE"}: if the previous line were empty, then print it, otherwise don't print (i.e. delete it). Then set the variable prev_empty to TRUE.

                                      • If the current line is not empty, {prev_empty = ""; print}: set the variable prev_empty to nothing, and print the line.


                                      The logic is that in almost all situations, print the current line. However, don't print the current line if the current line is empty (matches /^$/), and the previous line was non-empty (i.e. prev_empty = "").






                                      share|improve this answer


























                                        0












                                        0








                                        0






                                        awk 'BEGIN {prev_empty = "TRUE"}; {if (/^$/) {if (prev_empty) {print}; prev_empty = "TRUE"} else {prev_empty = ""; print}}' file.txt


                                        Explanation





                                        • BEGIN {prev_empty = "TRUE"}: start by initialising the variable prev_empty to TRUE.


                                        • if (/^$/): check if the line is empty (i.e. matches the regex /^$/).

                                        • If so, then {if (prev_empty == "TRUE") {print}; prev_empty = "TRUE"}: if the previous line were empty, then print it, otherwise don't print (i.e. delete it). Then set the variable prev_empty to TRUE.

                                        • If the current line is not empty, {prev_empty = ""; print}: set the variable prev_empty to nothing, and print the line.


                                        The logic is that in almost all situations, print the current line. However, don't print the current line if the current line is empty (matches /^$/), and the previous line was non-empty (i.e. prev_empty = "").






                                        share|improve this answer














                                        awk 'BEGIN {prev_empty = "TRUE"}; {if (/^$/) {if (prev_empty) {print}; prev_empty = "TRUE"} else {prev_empty = ""; print}}' file.txt


                                        Explanation





                                        • BEGIN {prev_empty = "TRUE"}: start by initialising the variable prev_empty to TRUE.


                                        • if (/^$/): check if the line is empty (i.e. matches the regex /^$/).

                                        • If so, then {if (prev_empty == "TRUE") {print}; prev_empty = "TRUE"}: if the previous line were empty, then print it, otherwise don't print (i.e. delete it). Then set the variable prev_empty to TRUE.

                                        • If the current line is not empty, {prev_empty = ""; print}: set the variable prev_empty to nothing, and print the line.


                                        The logic is that in almost all situations, print the current line. However, don't print the current line if the current line is empty (matches /^$/), and the previous line was non-empty (i.e. prev_empty = "").







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited 18 hours ago

























                                        answered 19 hours ago









                                        Sparhawk

                                        9,33263991




                                        9,33263991























                                            0














                                            If the file is small enough to fit in memory, you can slurp the file with perl and remove any newline that comes after a non-space character and another newline:



                                            $ perl -0pe 's/(Sn)n/$1/g' file 
                                            this is testing 1
                                            this is testing 2

                                            this is testing 4





                                            share|improve this answer


























                                              0














                                              If the file is small enough to fit in memory, you can slurp the file with perl and remove any newline that comes after a non-space character and another newline:



                                              $ perl -0pe 's/(Sn)n/$1/g' file 
                                              this is testing 1
                                              this is testing 2

                                              this is testing 4





                                              share|improve this answer
























                                                0












                                                0








                                                0






                                                If the file is small enough to fit in memory, you can slurp the file with perl and remove any newline that comes after a non-space character and another newline:



                                                $ perl -0pe 's/(Sn)n/$1/g' file 
                                                this is testing 1
                                                this is testing 2

                                                this is testing 4





                                                share|improve this answer












                                                If the file is small enough to fit in memory, you can slurp the file with perl and remove any newline that comes after a non-space character and another newline:



                                                $ perl -0pe 's/(Sn)n/$1/g' file 
                                                this is testing 1
                                                this is testing 2

                                                this is testing 4






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered 17 hours ago









                                                terdon

                                                128k31249424




                                                128k31249424























                                                    0














                                                    With Awk, you could set the record separator to double-newline and then print each record with the default single newline output record separator.



                                                    With POSIX Awk, that will result in an extra newline after the last record - if you have GNU Awk (gawk) you can prevent that by setting an empty output record separator if the actual input record separator is empty:



                                                    $ gawk -vRS='nn' 'RT=="" {ORS=""} 1' file.txt
                                                    this is testing 1
                                                    this is testing 2

                                                    this is testing 4





                                                    share|improve this answer


























                                                      0














                                                      With Awk, you could set the record separator to double-newline and then print each record with the default single newline output record separator.



                                                      With POSIX Awk, that will result in an extra newline after the last record - if you have GNU Awk (gawk) you can prevent that by setting an empty output record separator if the actual input record separator is empty:



                                                      $ gawk -vRS='nn' 'RT=="" {ORS=""} 1' file.txt
                                                      this is testing 1
                                                      this is testing 2

                                                      this is testing 4





                                                      share|improve this answer
























                                                        0












                                                        0








                                                        0






                                                        With Awk, you could set the record separator to double-newline and then print each record with the default single newline output record separator.



                                                        With POSIX Awk, that will result in an extra newline after the last record - if you have GNU Awk (gawk) you can prevent that by setting an empty output record separator if the actual input record separator is empty:



                                                        $ gawk -vRS='nn' 'RT=="" {ORS=""} 1' file.txt
                                                        this is testing 1
                                                        this is testing 2

                                                        this is testing 4





                                                        share|improve this answer












                                                        With Awk, you could set the record separator to double-newline and then print each record with the default single newline output record separator.



                                                        With POSIX Awk, that will result in an extra newline after the last record - if you have GNU Awk (gawk) you can prevent that by setting an empty output record separator if the actual input record separator is empty:



                                                        $ gawk -vRS='nn' 'RT=="" {ORS=""} 1' file.txt
                                                        this is testing 1
                                                        this is testing 2

                                                        this is testing 4






                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered 13 hours ago









                                                        steeldriver

                                                        34.5k35083




                                                        34.5k35083






















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










                                                            draft saved

                                                            draft discarded


















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













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












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
















                                                            Thanks for contributing an answer to Unix & Linux Stack Exchange!


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





                                                            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                            Please pay close attention to the following guidance:


                                                            • 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%2funix.stackexchange.com%2fquestions%2f492410%2fdelete-empty-line-if-there-is-only-one-between-two-lines-if-there-are-two-empty%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 to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

                                                            is 'sed' thread safe

                                                            How to make a Squid Proxy server?