Write a shell script to calculate the sum from a given 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 A1 23
2 B1 21
3 C1 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 steps.

Steps1-find the all item start with A along with it’s cost and write the output to another file(filter_data.txt)
awk -F , ‘$2~/^c/’ Items.csv >filter_data.txt

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

 

You May Also Like

About the Author: Nitesh

I am a software engineer and Enthusiastic to learn new things

Leave a Reply

Your email address will not be published. Required fields are marked *