Using awk from bash, how to execute multiple statements in “if” clause?
Clash Royale CLAN TAG#URR8PPP
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
add a comment |
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
if (…) …
might help :-)
– nohillside
Feb 11 at 20:23
likeif($6 == "MALE") count+=1; print $2
?
– Z E Nir
Feb 11 at 20:25
add a comment |
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
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
awk
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
likeif($6 == "MALE") count+=1; print $2
?
– Z E Nir
Feb 11 at 20:25
add a comment |
if (…) …
might help :-)
– nohillside
Feb 11 at 20:23
likeif($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
add a comment |
3 Answers
3
active
oldest
votes
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.
add a comment |
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.
add a comment |
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
Thanks for the explanation
– Z E Nir
Feb 11 at 20:35
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Feb 11 at 20:25
JRFergusonJRFerguson
10.2k32431
10.2k32431
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Feb 12 at 22:52
answered Feb 11 at 21:49
KusalanandaKusalananda
134k17255418
134k17255418
add a comment |
add a comment |
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
Thanks for the explanation
– Z E Nir
Feb 11 at 20:35
add a comment |
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
Thanks for the explanation
– Z E Nir
Feb 11 at 20:35
add a comment |
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
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
answered Feb 11 at 20:28
community wiki
glenn jackman
Thanks for the explanation
– Z E Nir
Feb 11 at 20:35
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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