Using awk from bash, how to execute multiple statements in “if” clause?

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












0















Consider the following file (datafile):



1001 Alice Rotterdam Netherland 48 FEMALE
1002 Bob Brussels Belgium 13 MALE
1003 Carol Tel-Aviv Israel 20 FEMALE
1004 Dee Manhattan USA 17 FEMALE
1005 Euler Paris French 71 MALE
1006 Fiona Paris French 12 MALE
1007 Gordon Moscow Russia 34 MALE
1008 Hana Kanto Japan 24 FEMALE
1009 Ivan Crimea Ukraine 30 MALE
1010 Jenora Crimea Ukraine 25 FENALE


I want to count all the records that represent a male and print the men names, and I need to do it from Bash. How awk can deal with two statements in the 'if' clause?



Here is what I got:



awk 'BEGINcount = 0 if($6 == "MALE") count+=1; print $2 ENDprint count' datafile



and the output is:



Alice 
Bob
Carol
Dee
Euler
Fiona
Gordon
Hana
Ivan
Jenora
5


As you can see, the counting is successful, but the program print the name of all the records, since the if clause stops right after the first statement.










share|improve this question
























  • if (…) … might help :-)

    – nohillside
    Feb 11 at 20:23











  • like if($6 == "MALE") count+=1; print $2 ?

    – Z E Nir
    Feb 11 at 20:25















0















Consider the following file (datafile):



1001 Alice Rotterdam Netherland 48 FEMALE
1002 Bob Brussels Belgium 13 MALE
1003 Carol Tel-Aviv Israel 20 FEMALE
1004 Dee Manhattan USA 17 FEMALE
1005 Euler Paris French 71 MALE
1006 Fiona Paris French 12 MALE
1007 Gordon Moscow Russia 34 MALE
1008 Hana Kanto Japan 24 FEMALE
1009 Ivan Crimea Ukraine 30 MALE
1010 Jenora Crimea Ukraine 25 FENALE


I want to count all the records that represent a male and print the men names, and I need to do it from Bash. How awk can deal with two statements in the 'if' clause?



Here is what I got:



awk 'BEGINcount = 0 if($6 == "MALE") count+=1; print $2 ENDprint count' datafile



and the output is:



Alice 
Bob
Carol
Dee
Euler
Fiona
Gordon
Hana
Ivan
Jenora
5


As you can see, the counting is successful, but the program print the name of all the records, since the if clause stops right after the first statement.










share|improve this question
























  • if (…) … might help :-)

    – nohillside
    Feb 11 at 20:23











  • like if($6 == "MALE") count+=1; print $2 ?

    – Z E Nir
    Feb 11 at 20:25













0












0








0








Consider the following file (datafile):



1001 Alice Rotterdam Netherland 48 FEMALE
1002 Bob Brussels Belgium 13 MALE
1003 Carol Tel-Aviv Israel 20 FEMALE
1004 Dee Manhattan USA 17 FEMALE
1005 Euler Paris French 71 MALE
1006 Fiona Paris French 12 MALE
1007 Gordon Moscow Russia 34 MALE
1008 Hana Kanto Japan 24 FEMALE
1009 Ivan Crimea Ukraine 30 MALE
1010 Jenora Crimea Ukraine 25 FENALE


I want to count all the records that represent a male and print the men names, and I need to do it from Bash. How awk can deal with two statements in the 'if' clause?



Here is what I got:



awk 'BEGINcount = 0 if($6 == "MALE") count+=1; print $2 ENDprint count' datafile



and the output is:



Alice 
Bob
Carol
Dee
Euler
Fiona
Gordon
Hana
Ivan
Jenora
5


As you can see, the counting is successful, but the program print the name of all the records, since the if clause stops right after the first statement.










share|improve this question
















Consider the following file (datafile):



1001 Alice Rotterdam Netherland 48 FEMALE
1002 Bob Brussels Belgium 13 MALE
1003 Carol Tel-Aviv Israel 20 FEMALE
1004 Dee Manhattan USA 17 FEMALE
1005 Euler Paris French 71 MALE
1006 Fiona Paris French 12 MALE
1007 Gordon Moscow Russia 34 MALE
1008 Hana Kanto Japan 24 FEMALE
1009 Ivan Crimea Ukraine 30 MALE
1010 Jenora Crimea Ukraine 25 FENALE


I want to count all the records that represent a male and print the men names, and I need to do it from Bash. How awk can deal with two statements in the 'if' clause?



Here is what I got:



awk 'BEGINcount = 0 if($6 == "MALE") count+=1; print $2 ENDprint count' datafile



and the output is:



Alice 
Bob
Carol
Dee
Euler
Fiona
Gordon
Hana
Ivan
Jenora
5


As you can see, the counting is successful, but the program print the name of all the records, since the if clause stops right after the first statement.







awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 11 at 20:24









Rui F Ribeiro

41.1k1480138




41.1k1480138










asked Feb 11 at 20:18









Z E NirZ E Nir

56119




56119












  • if (…) … might help :-)

    – nohillside
    Feb 11 at 20:23











  • like if($6 == "MALE") count+=1; print $2 ?

    – Z E Nir
    Feb 11 at 20:25

















  • if (…) … might help :-)

    – nohillside
    Feb 11 at 20:23











  • like if($6 == "MALE") count+=1; print $2 ?

    – Z E Nir
    Feb 11 at 20:25
















if (…) … might help :-)

– nohillside
Feb 11 at 20:23





if (…) … might help :-)

– nohillside
Feb 11 at 20:23













like if($6 == "MALE") count+=1; print $2 ?

– Z E Nir
Feb 11 at 20:25





like if($6 == "MALE") count+=1; print $2 ?

– Z E Nir
Feb 11 at 20:25










3 Answers
3






active

oldest

votes


















2














awk 'BEGINcount = 0 if($6 == "MALE") count+=1; print $2 ENDprint count' 


That is, create a block for the count and the printing of the name that is executed if the test is a male.






share|improve this answer






























    7














    The awk way to do this would be



    awk '$NF == "MALE" ++count; print $2 END print count ' file


    That is, you specify a number of blocks and the associated trigger conditions. The condition for incrementing count and for printing $2 is that the last field's value ($NF) is the string MALE. The condition for printing count is that we have no more input.



    You could have done this with an if statement too:



    awk ' if ($NF == "MALE") ++count; print $2 END print count ' file


    (note the extra set of curly braces in the first block; the if statement's body would otherwise only be the next single statement) but the program quickly becomes hard to read, and since the $NF == "MALE" condition applies to all the code in that block, we may as well move it out of the block completely.






    share|improve this answer
































      2














      Just to follow up on JRFerguson's answer, this is what you have, with indentation:



      # count the number of males, and print every name
      if($6 == "MALE")
      count+=1
      print $2


      And this is JRFergusons's answer, with indentation



      # count and print only the males
      if($6 == "MALE")
      count+=1
      print $2



      Awk uses the same bracing semantics as C






      share|improve this answer

























      • Thanks for the explanation

        – Z E Nir
        Feb 11 at 20:35










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



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f500028%2fusing-awk-from-bash-how-to-execute-multiple-statements-in-if-clause%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      awk 'BEGINcount = 0 if($6 == "MALE") count+=1; print $2 ENDprint count' 


      That is, create a block for the count and the printing of the name that is executed if the test is a male.






      share|improve this answer



























        2














        awk 'BEGINcount = 0 if($6 == "MALE") count+=1; print $2 ENDprint count' 


        That is, create a block for the count and the printing of the name that is executed if the test is a male.






        share|improve this answer

























          2












          2








          2







          awk 'BEGINcount = 0 if($6 == "MALE") count+=1; print $2 ENDprint count' 


          That is, create a block for the count and the printing of the name that is executed if the test is a male.






          share|improve this answer













          awk 'BEGINcount = 0 if($6 == "MALE") count+=1; print $2 ENDprint count' 


          That is, create a block for the count and the printing of the name that is executed if the test is a male.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 11 at 20:25









          JRFergusonJRFerguson

          10.2k32431




          10.2k32431























              7














              The awk way to do this would be



              awk '$NF == "MALE" ++count; print $2 END print count ' file


              That is, you specify a number of blocks and the associated trigger conditions. The condition for incrementing count and for printing $2 is that the last field's value ($NF) is the string MALE. The condition for printing count is that we have no more input.



              You could have done this with an if statement too:



              awk ' if ($NF == "MALE") ++count; print $2 END print count ' file


              (note the extra set of curly braces in the first block; the if statement's body would otherwise only be the next single statement) but the program quickly becomes hard to read, and since the $NF == "MALE" condition applies to all the code in that block, we may as well move it out of the block completely.






              share|improve this answer





























                7














                The awk way to do this would be



                awk '$NF == "MALE" ++count; print $2 END print count ' file


                That is, you specify a number of blocks and the associated trigger conditions. The condition for incrementing count and for printing $2 is that the last field's value ($NF) is the string MALE. The condition for printing count is that we have no more input.



                You could have done this with an if statement too:



                awk ' if ($NF == "MALE") ++count; print $2 END print count ' file


                (note the extra set of curly braces in the first block; the if statement's body would otherwise only be the next single statement) but the program quickly becomes hard to read, and since the $NF == "MALE" condition applies to all the code in that block, we may as well move it out of the block completely.






                share|improve this answer



























                  7












                  7








                  7







                  The awk way to do this would be



                  awk '$NF == "MALE" ++count; print $2 END print count ' file


                  That is, you specify a number of blocks and the associated trigger conditions. The condition for incrementing count and for printing $2 is that the last field's value ($NF) is the string MALE. The condition for printing count is that we have no more input.



                  You could have done this with an if statement too:



                  awk ' if ($NF == "MALE") ++count; print $2 END print count ' file


                  (note the extra set of curly braces in the first block; the if statement's body would otherwise only be the next single statement) but the program quickly becomes hard to read, and since the $NF == "MALE" condition applies to all the code in that block, we may as well move it out of the block completely.






                  share|improve this answer















                  The awk way to do this would be



                  awk '$NF == "MALE" ++count; print $2 END print count ' file


                  That is, you specify a number of blocks and the associated trigger conditions. The condition for incrementing count and for printing $2 is that the last field's value ($NF) is the string MALE. The condition for printing count is that we have no more input.



                  You could have done this with an if statement too:



                  awk ' if ($NF == "MALE") ++count; print $2 END print count ' file


                  (note the extra set of curly braces in the first block; the if statement's body would otherwise only be the next single statement) but the program quickly becomes hard to read, and since the $NF == "MALE" condition applies to all the code in that block, we may as well move it out of the block completely.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 12 at 22:52

























                  answered Feb 11 at 21:49









                  KusalanandaKusalananda

                  134k17255418




                  134k17255418





















                      2














                      Just to follow up on JRFerguson's answer, this is what you have, with indentation:



                      # count the number of males, and print every name
                      if($6 == "MALE")
                      count+=1
                      print $2


                      And this is JRFergusons's answer, with indentation



                      # count and print only the males
                      if($6 == "MALE")
                      count+=1
                      print $2



                      Awk uses the same bracing semantics as C






                      share|improve this answer

























                      • Thanks for the explanation

                        – Z E Nir
                        Feb 11 at 20:35















                      2














                      Just to follow up on JRFerguson's answer, this is what you have, with indentation:



                      # count the number of males, and print every name
                      if($6 == "MALE")
                      count+=1
                      print $2


                      And this is JRFergusons's answer, with indentation



                      # count and print only the males
                      if($6 == "MALE")
                      count+=1
                      print $2



                      Awk uses the same bracing semantics as C






                      share|improve this answer

























                      • Thanks for the explanation

                        – Z E Nir
                        Feb 11 at 20:35













                      2












                      2








                      2







                      Just to follow up on JRFerguson's answer, this is what you have, with indentation:



                      # count the number of males, and print every name
                      if($6 == "MALE")
                      count+=1
                      print $2


                      And this is JRFergusons's answer, with indentation



                      # count and print only the males
                      if($6 == "MALE")
                      count+=1
                      print $2



                      Awk uses the same bracing semantics as C






                      share|improve this answer















                      Just to follow up on JRFerguson's answer, this is what you have, with indentation:



                      # count the number of males, and print every name
                      if($6 == "MALE")
                      count+=1
                      print $2


                      And this is JRFergusons's answer, with indentation



                      # count and print only the males
                      if($6 == "MALE")
                      count+=1
                      print $2



                      Awk uses the same bracing semantics as C







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      answered Feb 11 at 20:28


























                      community wiki





                      glenn jackman













                      • Thanks for the explanation

                        – Z E Nir
                        Feb 11 at 20:35

















                      • Thanks for the explanation

                        – Z E Nir
                        Feb 11 at 20:35
















                      Thanks for the explanation

                      – Z E Nir
                      Feb 11 at 20:35





                      Thanks for the explanation

                      – Z E Nir
                      Feb 11 at 20:35

















                      draft saved

                      draft discarded
















































                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f500028%2fusing-awk-from-bash-how-to-execute-multiple-statements-in-if-clause%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 check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay