DEV Community


Discussion on: One-liner to sum up numbers from a CSV file

moopet profile image
Ben Sinclair

I like this, because there are loads of ways you could do it.

You could use cut -d, -f2 instead of the awk. You could use tr "\n" "+" instead of the sed to transform one character (though you might have to fudge that).

You can get rid of the cat and use tail +2 shopping.csv.

You can use grep to do almost all the heavy lifting if you want, because you can tell it to only print the matching text with -o, which has the added benefit of skipping the first line entirely so you no longer need the cat or the tail.

I'd go with this combo, which uses paste to join the lines:

grep -o "[0-9.]\+" shopping.csv | paste -s -d+ | bc
Enter fullscreen mode Exit fullscreen mode