grep uncommented and empty lines in linux












0














I use below command to filter the file for # and empty lines.
But, how can we grep uncommented and blank lines in linux with single grep.



[root@localhost ~]# cat test | grep -v ^# | grep -v ^$









share|improve this question





























    0














    I use below command to filter the file for # and empty lines.
    But, how can we grep uncommented and blank lines in linux with single grep.



    [root@localhost ~]# cat test | grep -v ^# | grep -v ^$









    share|improve this question



























      0












      0








      0







      I use below command to filter the file for # and empty lines.
      But, how can we grep uncommented and blank lines in linux with single grep.



      [root@localhost ~]# cat test | grep -v ^# | grep -v ^$









      share|improve this question















      I use below command to filter the file for # and empty lines.
      But, how can we grep uncommented and blank lines in linux with single grep.



      [root@localhost ~]# cat test | grep -v ^# | grep -v ^$






      linux grep






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 11 at 18:18

























      asked Dec 11 at 11:45









      Ritesh Vishwakarma

      163




      163






















          4 Answers
          4






          active

          oldest

          votes


















          1














          For simple cases, my go-to pattern for this is:



          $ egrep '^[^#]'


          This pattern matches lines which begin with some character OTHER than a pound sign.



          If you extend the definition of 'blank' line to include a line entirely of whitespace, then the pattern fails, as it will match such a line. The pattern also fails if you allow arbitrary whitespace before the pound sign in a comment line (as Apache, bash, and others do).



          If those cases are important to you, this pattern is better:



          $ egrep '^[[:blank:]]*[^[:blank:]#]'


          For example:



          $ cat test
          # comment
          # spaces then comment
          config # then comment
          before empty line

          after empty line
          space only on next line


          tab only on next line

          $ egrep '^[[:blank:]]*[^[:blank:]#]' test
          config # then comment
          before empty line
          after empty line
          space only on next line
          tab only on next line
          $





          share|improve this answer





























            1














            IIUC, you want to show non-blank and non-commented lines. You can do that with -e using a single grep command:



            grep -v -e "^#" -e "^$" test


            For example, if your test looks like this:



            #a
            uncommented line

            #comment below blank line


            output will be:



            $ grep -v -e "^#" -e "^$" test
            uncommented line





            share|improve this answer





















            • thanks for your response, but i am looking to do it in single go. like below(though it doesn't work) cat test | grep -v ^[$,#]
              – Ritesh Vishwakarma
              Dec 11 at 11:56



















            1














            consider the file:



            valid config line 1
            # Comment
            valid config line 2 # Comment
            Blank line between

            These two
            One space in the line between

            These two


            If you consider the cases:




            • Lines starting with #

            • Blank lines


            You can use cat file | grep -v '^$|^#' or cat file | grep -v '^($|#)'



            And you will get something like:



            valid config line 1
            valid config line 2 # Comment
            Blank line between
            These two
            One space in the line between

            These two


            However, I would consider also lines that start with a config line and then have an inline comment (supported in several config files), lines that are not blank but have only spaces, for this in a single command, I would use sed:



            cat file | sed '/^(#|[[:space:]]*$)/d;s/#.*//g'


            Obtaining:



            valid config line 1
            valid config line 2
            Blank line between
            These two
            One space in the line between
            These two


            Explanation





            • [[:space:]]*$ matches 0 or more spaces before the end of the line


            • ^(a|b) matches lines starting with a or b, using # as a and [[:space:]]*$ as b will match all lines starting with #, blank lines, and lines that only have spaces.


            • /match/d deletes all the matching lines


            • ; separates sed commands


            • s/a/b/g replaces a with b globally. Using #.* as a and an empty b will remove all the matching comments after a line.


            Hope it helps. Regards






            share|improve this answer





























              0














              Got it!



              [root@localhost ~]# cat file | grep -v ^'$|#'





              share|improve this answer

















              • 3




                Hi Ritesh, watch out! if you have a line like config line # Comment you will be taking it out with this. If all of your comments are starting with # as the first char you should use grep -v '^$|^#'
                – Jorge Valentini
                Dec 11 at 16:32













              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "3"
              };
              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
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1382606%2fgrep-uncommented-and-empty-lines-in-linux%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              For simple cases, my go-to pattern for this is:



              $ egrep '^[^#]'


              This pattern matches lines which begin with some character OTHER than a pound sign.



              If you extend the definition of 'blank' line to include a line entirely of whitespace, then the pattern fails, as it will match such a line. The pattern also fails if you allow arbitrary whitespace before the pound sign in a comment line (as Apache, bash, and others do).



              If those cases are important to you, this pattern is better:



              $ egrep '^[[:blank:]]*[^[:blank:]#]'


              For example:



              $ cat test
              # comment
              # spaces then comment
              config # then comment
              before empty line

              after empty line
              space only on next line


              tab only on next line

              $ egrep '^[[:blank:]]*[^[:blank:]#]' test
              config # then comment
              before empty line
              after empty line
              space only on next line
              tab only on next line
              $





              share|improve this answer


























                1














                For simple cases, my go-to pattern for this is:



                $ egrep '^[^#]'


                This pattern matches lines which begin with some character OTHER than a pound sign.



                If you extend the definition of 'blank' line to include a line entirely of whitespace, then the pattern fails, as it will match such a line. The pattern also fails if you allow arbitrary whitespace before the pound sign in a comment line (as Apache, bash, and others do).



                If those cases are important to you, this pattern is better:



                $ egrep '^[[:blank:]]*[^[:blank:]#]'


                For example:



                $ cat test
                # comment
                # spaces then comment
                config # then comment
                before empty line

                after empty line
                space only on next line


                tab only on next line

                $ egrep '^[[:blank:]]*[^[:blank:]#]' test
                config # then comment
                before empty line
                after empty line
                space only on next line
                tab only on next line
                $





                share|improve this answer
























                  1












                  1








                  1






                  For simple cases, my go-to pattern for this is:



                  $ egrep '^[^#]'


                  This pattern matches lines which begin with some character OTHER than a pound sign.



                  If you extend the definition of 'blank' line to include a line entirely of whitespace, then the pattern fails, as it will match such a line. The pattern also fails if you allow arbitrary whitespace before the pound sign in a comment line (as Apache, bash, and others do).



                  If those cases are important to you, this pattern is better:



                  $ egrep '^[[:blank:]]*[^[:blank:]#]'


                  For example:



                  $ cat test
                  # comment
                  # spaces then comment
                  config # then comment
                  before empty line

                  after empty line
                  space only on next line


                  tab only on next line

                  $ egrep '^[[:blank:]]*[^[:blank:]#]' test
                  config # then comment
                  before empty line
                  after empty line
                  space only on next line
                  tab only on next line
                  $





                  share|improve this answer












                  For simple cases, my go-to pattern for this is:



                  $ egrep '^[^#]'


                  This pattern matches lines which begin with some character OTHER than a pound sign.



                  If you extend the definition of 'blank' line to include a line entirely of whitespace, then the pattern fails, as it will match such a line. The pattern also fails if you allow arbitrary whitespace before the pound sign in a comment line (as Apache, bash, and others do).



                  If those cases are important to you, this pattern is better:



                  $ egrep '^[[:blank:]]*[^[:blank:]#]'


                  For example:



                  $ cat test
                  # comment
                  # spaces then comment
                  config # then comment
                  before empty line

                  after empty line
                  space only on next line


                  tab only on next line

                  $ egrep '^[[:blank:]]*[^[:blank:]#]' test
                  config # then comment
                  before empty line
                  after empty line
                  space only on next line
                  tab only on next line
                  $






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 11 at 21:04









                  Jim L.

                  1413




                  1413

























                      1














                      IIUC, you want to show non-blank and non-commented lines. You can do that with -e using a single grep command:



                      grep -v -e "^#" -e "^$" test


                      For example, if your test looks like this:



                      #a
                      uncommented line

                      #comment below blank line


                      output will be:



                      $ grep -v -e "^#" -e "^$" test
                      uncommented line





                      share|improve this answer





















                      • thanks for your response, but i am looking to do it in single go. like below(though it doesn't work) cat test | grep -v ^[$,#]
                        – Ritesh Vishwakarma
                        Dec 11 at 11:56
















                      1














                      IIUC, you want to show non-blank and non-commented lines. You can do that with -e using a single grep command:



                      grep -v -e "^#" -e "^$" test


                      For example, if your test looks like this:



                      #a
                      uncommented line

                      #comment below blank line


                      output will be:



                      $ grep -v -e "^#" -e "^$" test
                      uncommented line





                      share|improve this answer





















                      • thanks for your response, but i am looking to do it in single go. like below(though it doesn't work) cat test | grep -v ^[$,#]
                        – Ritesh Vishwakarma
                        Dec 11 at 11:56














                      1












                      1








                      1






                      IIUC, you want to show non-blank and non-commented lines. You can do that with -e using a single grep command:



                      grep -v -e "^#" -e "^$" test


                      For example, if your test looks like this:



                      #a
                      uncommented line

                      #comment below blank line


                      output will be:



                      $ grep -v -e "^#" -e "^$" test
                      uncommented line





                      share|improve this answer












                      IIUC, you want to show non-blank and non-commented lines. You can do that with -e using a single grep command:



                      grep -v -e "^#" -e "^$" test


                      For example, if your test looks like this:



                      #a
                      uncommented line

                      #comment below blank line


                      output will be:



                      $ grep -v -e "^#" -e "^$" test
                      uncommented line






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Dec 11 at 11:53









                      Arkadiusz Drabczyk

                      1,571711




                      1,571711












                      • thanks for your response, but i am looking to do it in single go. like below(though it doesn't work) cat test | grep -v ^[$,#]
                        – Ritesh Vishwakarma
                        Dec 11 at 11:56


















                      • thanks for your response, but i am looking to do it in single go. like below(though it doesn't work) cat test | grep -v ^[$,#]
                        – Ritesh Vishwakarma
                        Dec 11 at 11:56
















                      thanks for your response, but i am looking to do it in single go. like below(though it doesn't work) cat test | grep -v ^[$,#]
                      – Ritesh Vishwakarma
                      Dec 11 at 11:56




                      thanks for your response, but i am looking to do it in single go. like below(though it doesn't work) cat test | grep -v ^[$,#]
                      – Ritesh Vishwakarma
                      Dec 11 at 11:56











                      1














                      consider the file:



                      valid config line 1
                      # Comment
                      valid config line 2 # Comment
                      Blank line between

                      These two
                      One space in the line between

                      These two


                      If you consider the cases:




                      • Lines starting with #

                      • Blank lines


                      You can use cat file | grep -v '^$|^#' or cat file | grep -v '^($|#)'



                      And you will get something like:



                      valid config line 1
                      valid config line 2 # Comment
                      Blank line between
                      These two
                      One space in the line between

                      These two


                      However, I would consider also lines that start with a config line and then have an inline comment (supported in several config files), lines that are not blank but have only spaces, for this in a single command, I would use sed:



                      cat file | sed '/^(#|[[:space:]]*$)/d;s/#.*//g'


                      Obtaining:



                      valid config line 1
                      valid config line 2
                      Blank line between
                      These two
                      One space in the line between
                      These two


                      Explanation





                      • [[:space:]]*$ matches 0 or more spaces before the end of the line


                      • ^(a|b) matches lines starting with a or b, using # as a and [[:space:]]*$ as b will match all lines starting with #, blank lines, and lines that only have spaces.


                      • /match/d deletes all the matching lines


                      • ; separates sed commands


                      • s/a/b/g replaces a with b globally. Using #.* as a and an empty b will remove all the matching comments after a line.


                      Hope it helps. Regards






                      share|improve this answer


























                        1














                        consider the file:



                        valid config line 1
                        # Comment
                        valid config line 2 # Comment
                        Blank line between

                        These two
                        One space in the line between

                        These two


                        If you consider the cases:




                        • Lines starting with #

                        • Blank lines


                        You can use cat file | grep -v '^$|^#' or cat file | grep -v '^($|#)'



                        And you will get something like:



                        valid config line 1
                        valid config line 2 # Comment
                        Blank line between
                        These two
                        One space in the line between

                        These two


                        However, I would consider also lines that start with a config line and then have an inline comment (supported in several config files), lines that are not blank but have only spaces, for this in a single command, I would use sed:



                        cat file | sed '/^(#|[[:space:]]*$)/d;s/#.*//g'


                        Obtaining:



                        valid config line 1
                        valid config line 2
                        Blank line between
                        These two
                        One space in the line between
                        These two


                        Explanation





                        • [[:space:]]*$ matches 0 or more spaces before the end of the line


                        • ^(a|b) matches lines starting with a or b, using # as a and [[:space:]]*$ as b will match all lines starting with #, blank lines, and lines that only have spaces.


                        • /match/d deletes all the matching lines


                        • ; separates sed commands


                        • s/a/b/g replaces a with b globally. Using #.* as a and an empty b will remove all the matching comments after a line.


                        Hope it helps. Regards






                        share|improve this answer
























                          1












                          1








                          1






                          consider the file:



                          valid config line 1
                          # Comment
                          valid config line 2 # Comment
                          Blank line between

                          These two
                          One space in the line between

                          These two


                          If you consider the cases:




                          • Lines starting with #

                          • Blank lines


                          You can use cat file | grep -v '^$|^#' or cat file | grep -v '^($|#)'



                          And you will get something like:



                          valid config line 1
                          valid config line 2 # Comment
                          Blank line between
                          These two
                          One space in the line between

                          These two


                          However, I would consider also lines that start with a config line and then have an inline comment (supported in several config files), lines that are not blank but have only spaces, for this in a single command, I would use sed:



                          cat file | sed '/^(#|[[:space:]]*$)/d;s/#.*//g'


                          Obtaining:



                          valid config line 1
                          valid config line 2
                          Blank line between
                          These two
                          One space in the line between
                          These two


                          Explanation





                          • [[:space:]]*$ matches 0 or more spaces before the end of the line


                          • ^(a|b) matches lines starting with a or b, using # as a and [[:space:]]*$ as b will match all lines starting with #, blank lines, and lines that only have spaces.


                          • /match/d deletes all the matching lines


                          • ; separates sed commands


                          • s/a/b/g replaces a with b globally. Using #.* as a and an empty b will remove all the matching comments after a line.


                          Hope it helps. Regards






                          share|improve this answer












                          consider the file:



                          valid config line 1
                          # Comment
                          valid config line 2 # Comment
                          Blank line between

                          These two
                          One space in the line between

                          These two


                          If you consider the cases:




                          • Lines starting with #

                          • Blank lines


                          You can use cat file | grep -v '^$|^#' or cat file | grep -v '^($|#)'



                          And you will get something like:



                          valid config line 1
                          valid config line 2 # Comment
                          Blank line between
                          These two
                          One space in the line between

                          These two


                          However, I would consider also lines that start with a config line and then have an inline comment (supported in several config files), lines that are not blank but have only spaces, for this in a single command, I would use sed:



                          cat file | sed '/^(#|[[:space:]]*$)/d;s/#.*//g'


                          Obtaining:



                          valid config line 1
                          valid config line 2
                          Blank line between
                          These two
                          One space in the line between
                          These two


                          Explanation





                          • [[:space:]]*$ matches 0 or more spaces before the end of the line


                          • ^(a|b) matches lines starting with a or b, using # as a and [[:space:]]*$ as b will match all lines starting with #, blank lines, and lines that only have spaces.


                          • /match/d deletes all the matching lines


                          • ; separates sed commands


                          • s/a/b/g replaces a with b globally. Using #.* as a and an empty b will remove all the matching comments after a line.


                          Hope it helps. Regards







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 11 at 17:51









                          Jorge Valentini

                          30229




                          30229























                              0














                              Got it!



                              [root@localhost ~]# cat file | grep -v ^'$|#'





                              share|improve this answer

















                              • 3




                                Hi Ritesh, watch out! if you have a line like config line # Comment you will be taking it out with this. If all of your comments are starting with # as the first char you should use grep -v '^$|^#'
                                – Jorge Valentini
                                Dec 11 at 16:32


















                              0














                              Got it!



                              [root@localhost ~]# cat file | grep -v ^'$|#'





                              share|improve this answer

















                              • 3




                                Hi Ritesh, watch out! if you have a line like config line # Comment you will be taking it out with this. If all of your comments are starting with # as the first char you should use grep -v '^$|^#'
                                – Jorge Valentini
                                Dec 11 at 16:32
















                              0












                              0








                              0






                              Got it!



                              [root@localhost ~]# cat file | grep -v ^'$|#'





                              share|improve this answer












                              Got it!



                              [root@localhost ~]# cat file | grep -v ^'$|#'






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Dec 11 at 12:03









                              Ritesh Vishwakarma

                              163




                              163








                              • 3




                                Hi Ritesh, watch out! if you have a line like config line # Comment you will be taking it out with this. If all of your comments are starting with # as the first char you should use grep -v '^$|^#'
                                – Jorge Valentini
                                Dec 11 at 16:32
















                              • 3




                                Hi Ritesh, watch out! if you have a line like config line # Comment you will be taking it out with this. If all of your comments are starting with # as the first char you should use grep -v '^$|^#'
                                – Jorge Valentini
                                Dec 11 at 16:32










                              3




                              3




                              Hi Ritesh, watch out! if you have a line like config line # Comment you will be taking it out with this. If all of your comments are starting with # as the first char you should use grep -v '^$|^#'
                              – Jorge Valentini
                              Dec 11 at 16:32






                              Hi Ritesh, watch out! if you have a line like config line # Comment you will be taking it out with this. If all of your comments are starting with # as the first char you should use grep -v '^$|^#'
                              – Jorge Valentini
                              Dec 11 at 16:32




















                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Super User!


                              • 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%2fsuperuser.com%2fquestions%2f1382606%2fgrep-uncommented-and-empty-lines-in-linux%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

                              "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                              Alcedinidae

                              Origin of the phrase “under your belt”?