I have a CSV file with values like the following:


I want to fetch the values rows from this CSV file in such a way that the following sample conditions are met:

1) if (col7="Active" ) then sum of col1 values from all the rows

2) if (col6 == somevalue && column5 != somevalues) then sum of col1 values from all the rows.

I am able to fetch the rows from CSV file based on one column values which meets my first condition with the following awk command:

awk -F, '$7 == "Active" { print }' test.csv

How can i use multiple conditions in this awk command which checks multiple column values as mentioned in above sample condition 2 where i need to check present of some specific text in col6 and at the same time i need to ignore some keywords from column5. Is there a better and easier way of doing this instead of awk?


From The AWK Programming Language:

The simplest awk program is a sequence of pattern-action statements:
pattern { action }
pattern { action }

So, you can try following:

awk -F, '
         $7 == "Active" { x += $1; };
         $6 == "value6" && $5 != value5 { y += $1; };
         END { print x, y; }
        ' file

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.