2

I have a CSV file with values like the following:

col1,col2,col3,col4,col5,col6,col7
1,0,0,0,0,BTS,Active
4,5,3,1,1,LocalMode,Offlne
1,2,5,6,3,PermFault,Offline
1,2,6,6,2,BSC,Active
7,8,2,3,2,NE,Offline
1,7,6,5,2,BSC,Active

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?

1 Answer 1

2

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .