One-liner to sum up numbers from a CSV file

bgord

Bartosz Gordon

Posted on June 7, 2021

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Output:

rice,2.49
potatos,1.49
pasta,3.79
Enter fullscreen mode Exit fullscreen mode

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}'
Enter fullscreen mode Exit fullscreen mode

Output:

2.49
1.49
3.79
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Output:

2.49 1.49 3.79
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

Output:

2.49+1.49+3.79
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Output:

7.77
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
bgord
Bartosz Gordon

Posted on June 7, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related