I want to counts number of rows with certain columns equal to specific value. Below is a section of the input file:

enter image description here

The first row is the header. I want to count the number of rows that column 1 equal to 2002, column 3 equal to Female, column 4 equal to 18--20, column 8 equal to CO.

I tried the below command:

awk  -F "," '$1=="2002" && $3="Female"&& $4="18--20" && $8 ="CO"{count++};END{print count;}' newData.csv

But it didn't work.

I updated the command to above but it still not working. The number it print out is 1116 which is incorrect. This is just the number that column 1 equal to 2002. I need to count the rows that satisfy all the conditions.

  • 2
    Please don't post data as image, rather copy paste and format it as code {} – guillermo chamorro Oct 30 '19 at 14:34
  • Please don't update your original command after someone posted an answer that makes the answer look stupid. That is not nice. Please add the update in your answer below as additional text. – pLumo Oct 30 '19 at 14:41
  • 2
    And also regard the second sentence of the answer ... = does not equal == in awk. – pLumo Oct 30 '19 at 14:42
  • 2

awk columns are 1-indexed. $0 is the full line.

Also, you used = instead of == several times.

You need:

awk  -F "," '$1==2002 && $3=="Female" && $4=="18--20" && $8=="CO"{count++};END{print count;}' newData.csv 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.