Houssem Hariz
Houssem Hariz

Reputation: 55

shell script to sum column with if else

I would like to sum the second column of a CSV file.

I used the code below:

awk -F'#' '{c+=$2}END{printf "count=%d\n",c}' file.csv

It works perfectly. Now I would like to add ifelse condition to it. I would like to sum column 2 only if colum 3 = 'A' and Column 4 = 'B'

I add an ifelse clause but it doesn't work

awk -F'#' 'if [ "$3" == 'A' && "$4" == 'B' ]; then c+=$2; fi
END{printf "count=%d\n",c}' file.csv

Upvotes: 1

Views: 1655

Answers (1)

Inian
Inian

Reputation: 85550

You are mixing constructs from Awk and bash together. Assuming you want an if-else clause in Awk you need to do,

awk -F'#' '$3 == "A" && $4 == "B"{c+=$2;} END{printf "count=%d\n",c}' file.csv
count=41

for a sample input I produced as

$ cat file.csv
junk#21#A#B
junk#22#C#D
junk#20#A#B
junk#19#D#E

i.e. the statment '$3 == "A" && $4 == "B" implicitly means do the action of sum calculation only if $3 equals A and $4 equals B

Explicit usage of if can be done as something below,

awk -F'#' '{if ($3 == "A" && $4 == "B") {c+=$2;}} END{printf "count=%d\n",c}' file
count=41

It is NOT recommended to use a pure bash script for parsing/looping over files, but for mere practice if you want a way, you can do something like

#!/bin/bash

unset count 
# The '_' in read implies ignore rest of the line
while IFS='#' read -r  col1 col2 col3 col4 _
do
    [[ "$col3" == "A" && "$col4" == "B" ]] && ((count+=col2))
    # Here the '[[' implies the if-clause and '&&' implies do on true condition
done<file

printf "%d\n" "$count"

Upvotes: 4

Related Questions