In this ultimate guide to summing values in CSV files using shell scripts, we’ll walk through the process step by step, demonstrating how to calculate the sum of the cost of items that start with the letter ‘A’ in a CSV file.
Given a csv file name as Items in CSV format with the below contents. Write a shell script to calculate the sum of Cost of all Items that have name starting with “A” .
Sr No. | Item | Cost |
1 | c1 | 23 |
2 | b1 | 21 |
3 | b1 | 40 |
4 | abc | 123 |
5 | cba | 27 |
6 | nil | 56 |
There are multiple approach to solve this problem but in this post we follow below 2 simple steps.
You can explore more about AWK in the official AWK manual.
Steps1-find the all item start with c along with it’s cost and write the output to another file(filter_data.txt)
awk -F , ‘$2~/^c/’ Items.csv >filter_data.txt
awk is abbreviated from the names of developer Aho,Weinberger and Kernighan .awk is a scripting language which is used for text processing. F flag sets the field separator for the input . dollar sign (‘ $ ‘) is refer to a field in an awk program, followed by the number of the field you want in above script we use $2 which refer the 2nd field of given file.
awk is very efficient in handling regular expression and we also used regular expression in our script to find out all the item which start with c in given csv file and write the result in filter_data.txt .If file is not present then it create automatically.
Steps2-Read the file created in steps 1 and write the sum of value back to filter_data.txt file.
awk -F , ‘{sum+=$3} END {print sum}’ Output.txt
In this step we use sum function which iterate over third field and add all the integer value and print the total sum value. This is a small example to demonstrate one of the way to sum the column of integer value from given csv file.
Conclusion:
This article provides the ultimate guide to summing values in CSV files using shell scripts, with a focus on calculating specific values.