Grep and remove lines with a blank field value

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I was wondering if there's any command to look for blank field values of a text file and remove the entire line.



Example





 Hello:Its Me
Hello:How are you
Hello:
Hello:Bye


And expected output



 Hello:Its Me
Hello:How are you
Hello:Bye






share|improve this question


















  • 3




    do add your own efforts to solve to question when asking... your previous question didn't get an answer, but somehow you've got it for this one.. :)
    – Sundeep
    Dec 13 '17 at 16:21














up vote
2
down vote

favorite












I was wondering if there's any command to look for blank field values of a text file and remove the entire line.



Example





 Hello:Its Me
Hello:How are you
Hello:
Hello:Bye


And expected output



 Hello:Its Me
Hello:How are you
Hello:Bye






share|improve this question


















  • 3




    do add your own efforts to solve to question when asking... your previous question didn't get an answer, but somehow you've got it for this one.. :)
    – Sundeep
    Dec 13 '17 at 16:21












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I was wondering if there's any command to look for blank field values of a text file and remove the entire line.



Example





 Hello:Its Me
Hello:How are you
Hello:
Hello:Bye


And expected output



 Hello:Its Me
Hello:How are you
Hello:Bye






share|improve this question














I was wondering if there's any command to look for blank field values of a text file and remove the entire line.



Example





 Hello:Its Me
Hello:How are you
Hello:
Hello:Bye


And expected output



 Hello:Its Me
Hello:How are you
Hello:Bye








share|improve this question













share|improve this question




share|improve this question








edited Dec 13 '17 at 20:57









terdon♦

122k28230403




122k28230403










asked Dec 13 '17 at 16:05









John Wick

112




112







  • 3




    do add your own efforts to solve to question when asking... your previous question didn't get an answer, but somehow you've got it for this one.. :)
    – Sundeep
    Dec 13 '17 at 16:21












  • 3




    do add your own efforts to solve to question when asking... your previous question didn't get an answer, but somehow you've got it for this one.. :)
    – Sundeep
    Dec 13 '17 at 16:21







3




3




do add your own efforts to solve to question when asking... your previous question didn't get an answer, but somehow you've got it for this one.. :)
– Sundeep
Dec 13 '17 at 16:21




do add your own efforts to solve to question when asking... your previous question didn't get an answer, but somehow you've got it for this one.. :)
– Sundeep
Dec 13 '17 at 16:21










4 Answers
4






active

oldest

votes

















up vote
5
down vote













Very standard case, the simplest solution with awk would be



awk -F: '$2 != ""' file


and with grep:



grep :. file 





share|improve this answer





























    up vote
    3
    down vote













    If you mean to skip Hello: lines you can use something like:



    awk -F: 'if($2 != "") print ' input_file 





    share|improve this answer



























      up vote
      2
      down vote













      At least with GNU awk, and I think with any version of awk, the default action when something evaluates to true is to print. So, if you set the field delimiter to :, to print lines that have a second field, all you need is:



      $ awk -F: '$2' file
      Hello:Its Me
      Hello:How are you
      Hello:Bye


      However, this will also print cases where the second field is whitespace (a space, or a tab etc), but will not print the line if second field is one or multiple 0s, because awk evaluates this as false.






      share|improve this answer





























        up vote
        0
        down vote













        Sometimes it is easier to define the remove conditions (grep -v). Example remove when last field is empty:



        grep -v ':$' file





        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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f410679%2fgrep-and-remove-lines-with-a-blank-field-value%23new-answer', 'question_page');

          );

          Post as a guest






























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          5
          down vote













          Very standard case, the simplest solution with awk would be



          awk -F: '$2 != ""' file


          and with grep:



          grep :. file 





          share|improve this answer


























            up vote
            5
            down vote













            Very standard case, the simplest solution with awk would be



            awk -F: '$2 != ""' file


            and with grep:



            grep :. file 





            share|improve this answer
























              up vote
              5
              down vote










              up vote
              5
              down vote









              Very standard case, the simplest solution with awk would be



              awk -F: '$2 != ""' file


              and with grep:



              grep :. file 





              share|improve this answer














              Very standard case, the simplest solution with awk would be



              awk -F: '$2 != ""' file


              and with grep:



              grep :. file 






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Dec 13 '17 at 16:15









              Stéphane Chazelas

              282k53520854




              282k53520854










              answered Dec 13 '17 at 16:10









              jimmij

              28.9k867100




              28.9k867100






















                  up vote
                  3
                  down vote













                  If you mean to skip Hello: lines you can use something like:



                  awk -F: 'if($2 != "") print ' input_file 





                  share|improve this answer
























                    up vote
                    3
                    down vote













                    If you mean to skip Hello: lines you can use something like:



                    awk -F: 'if($2 != "") print ' input_file 





                    share|improve this answer






















                      up vote
                      3
                      down vote










                      up vote
                      3
                      down vote









                      If you mean to skip Hello: lines you can use something like:



                      awk -F: 'if($2 != "") print ' input_file 





                      share|improve this answer












                      If you mean to skip Hello: lines you can use something like:



                      awk -F: 'if($2 != "") print ' input_file 






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Dec 13 '17 at 16:08









                      Romeo Ninov

                      4,36811625




                      4,36811625




















                          up vote
                          2
                          down vote













                          At least with GNU awk, and I think with any version of awk, the default action when something evaluates to true is to print. So, if you set the field delimiter to :, to print lines that have a second field, all you need is:



                          $ awk -F: '$2' file
                          Hello:Its Me
                          Hello:How are you
                          Hello:Bye


                          However, this will also print cases where the second field is whitespace (a space, or a tab etc), but will not print the line if second field is one or multiple 0s, because awk evaluates this as false.






                          share|improve this answer


























                            up vote
                            2
                            down vote













                            At least with GNU awk, and I think with any version of awk, the default action when something evaluates to true is to print. So, if you set the field delimiter to :, to print lines that have a second field, all you need is:



                            $ awk -F: '$2' file
                            Hello:Its Me
                            Hello:How are you
                            Hello:Bye


                            However, this will also print cases where the second field is whitespace (a space, or a tab etc), but will not print the line if second field is one or multiple 0s, because awk evaluates this as false.






                            share|improve this answer
























                              up vote
                              2
                              down vote










                              up vote
                              2
                              down vote









                              At least with GNU awk, and I think with any version of awk, the default action when something evaluates to true is to print. So, if you set the field delimiter to :, to print lines that have a second field, all you need is:



                              $ awk -F: '$2' file
                              Hello:Its Me
                              Hello:How are you
                              Hello:Bye


                              However, this will also print cases where the second field is whitespace (a space, or a tab etc), but will not print the line if second field is one or multiple 0s, because awk evaluates this as false.






                              share|improve this answer














                              At least with GNU awk, and I think with any version of awk, the default action when something evaluates to true is to print. So, if you set the field delimiter to :, to print lines that have a second field, all you need is:



                              $ awk -F: '$2' file
                              Hello:Its Me
                              Hello:How are you
                              Hello:Bye


                              However, this will also print cases where the second field is whitespace (a space, or a tab etc), but will not print the line if second field is one or multiple 0s, because awk evaluates this as false.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Dec 13 '17 at 23:04









                              jimmij

                              28.9k867100




                              28.9k867100










                              answered Dec 13 '17 at 20:59









                              terdon♦

                              122k28230403




                              122k28230403




















                                  up vote
                                  0
                                  down vote













                                  Sometimes it is easier to define the remove conditions (grep -v). Example remove when last field is empty:



                                  grep -v ':$' file





                                  share|improve this answer
























                                    up vote
                                    0
                                    down vote













                                    Sometimes it is easier to define the remove conditions (grep -v). Example remove when last field is empty:



                                    grep -v ':$' file





                                    share|improve this answer






















                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      Sometimes it is easier to define the remove conditions (grep -v). Example remove when last field is empty:



                                      grep -v ':$' file





                                      share|improve this answer












                                      Sometimes it is easier to define the remove conditions (grep -v). Example remove when last field is empty:



                                      grep -v ':$' file






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 14 '17 at 10:17









                                      JJoao

                                      6,7211826




                                      6,7211826






















                                           

                                          draft saved


                                          draft discarded


























                                           


                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f410679%2fgrep-and-remove-lines-with-a-blank-field-value%23new-answer', 'question_page');

                                          );

                                          Post as a guest













































































                                          Popular posts from this blog

                                          How to check contact read email or not when send email to Individual?

                                          Bahrain

                                          Postfix configuration issue with fips on centos 7; mailgun relay