One-liner to sum up numbers from a CSV file

This is the shopping.csv CSV file that we will be working with throughout this blog post.

item,price
rice,2.49
potatos,1.49
pasta,3.79

The goal is to create a one-line bash script that prints the sum of the prices in the file above, which is 7…


This content originally appeared on DEV Community and was authored by Bartosz Gordon

This is the shopping.csv CSV file that we will be working with throughout this blog post.

item,price
rice,2.49
potatos,1.49
pasta,3.79

The goal is to create a one-line bash script that prints the sum of the prices in the file above, which is 7.77.

Step: 1. Print the file content

$ cat shopping.csv

This command prints the file content without modifications.
It gives us a way to redirect - pipe - the output to another command in the next step.

Output:

item,price
rice,2.49
potatos,1.49
pasta,3.79

Step: 2. Cut the first line

There are many ways to achieve it, but we use the tail +n syntax.

In our case, it takes all the lines until the end of the file, starting from the second line.

$ cat shopping.csv | tail +2

Output:

rice,2.49
potatos,1.49
pasta,3.79

Step: 3. Cut the first column

awk splits each line by a separator defined by the option -F.
In our case, it's the comma.
Then it prints the second column from every row.

$ cat shopping.csv | tail +2 | awk -F , '{print $2}'

Output:

2.49
1.49
3.79

Step: 4. Concatenate the prices

xargs is used to "squash" the lines into a single string, separating them by space.

$ cat shopping.csv | tail +2 | awk -F , '{print $2}' | xargs

Output:

2.49 1.49 3.79

Step: 5. Replace spaces with pluses

The sed expression replaces the escaped space with + in the entire string (globally - g).

$ cat shopping.csv | tail +2 | awk -F , '{print $2}' | xargs | sed -e 's/\ /+/g'

Output:

2.49+1.49+3.79

Step: 6. Perform the calculation

bc is a simple calculator that you can use interactively or by piping an equation into it.

$ cat shopping.csv | tail +2 | awk -F , '{print $2}' | xargs | sed -e 's/\ /+/g' | bc

Output:

7.77


This content originally appeared on DEV Community and was authored by Bartosz Gordon


Print Share Comment Cite Upload Translate Updates
APA

Bartosz Gordon | Sciencx (2021-06-07T20:18:03+00:00) One-liner to sum up numbers from a CSV file. Retrieved from https://www.scien.cx/2021/06/07/one-liner-to-sum-up-numbers-from-a-csv-file/

MLA
" » One-liner to sum up numbers from a CSV file." Bartosz Gordon | Sciencx - Monday June 7, 2021, https://www.scien.cx/2021/06/07/one-liner-to-sum-up-numbers-from-a-csv-file/
HARVARD
Bartosz Gordon | Sciencx Monday June 7, 2021 » One-liner to sum up numbers from a CSV file., viewed ,<https://www.scien.cx/2021/06/07/one-liner-to-sum-up-numbers-from-a-csv-file/>
VANCOUVER
Bartosz Gordon | Sciencx - » One-liner to sum up numbers from a CSV file. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/07/one-liner-to-sum-up-numbers-from-a-csv-file/
CHICAGO
" » One-liner to sum up numbers from a CSV file." Bartosz Gordon | Sciencx - Accessed . https://www.scien.cx/2021/06/07/one-liner-to-sum-up-numbers-from-a-csv-file/
IEEE
" » One-liner to sum up numbers from a CSV file." Bartosz Gordon | Sciencx [Online]. Available: https://www.scien.cx/2021/06/07/one-liner-to-sum-up-numbers-from-a-csv-file/. [Accessed: ]
rf:citation
» One-liner to sum up numbers from a CSV file | Bartosz Gordon | Sciencx | https://www.scien.cx/2021/06/07/one-liner-to-sum-up-numbers-from-a-csv-file/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.