1

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
4
  • 1
    You are mixing constructs from Awk and shell together. Commented Jan 23, 2017 at 9:13
  • Yes. I am noob on shell and Linux function. I only want to sum the second column with if clause. is that possible ? Commented Jan 23, 2017 at 9:17
  • Post some sample data please. Commented Jan 23, 2017 at 9:18
  • Read Effective Awk Programming, 4th Edition, by Arnold Robbins to learn awk, and Shell Scripting Recipes by Chris Johnson to learn shell. They are 2 completely different tools, each with their own purpose and syntax. Commented Jan 23, 2017 at 16:53

1 Answer 1

4

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"
Sign up to request clarification or add additional context in comments.

2 Comments

It is typically not recommended for performance: bash is horrifically slow
....and horrifically difficult to get the syntax right for text processing sine the default behaviors are designed to ease process and file manipulation, NOT text processing. See why-is-using-a-shell-loop-to-process-text-considered-bad-practice

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.