Carl Burks is a software developer for a global financial institution. With over ten years experience in technology and software development for financial organizations and over twenty years of software experience, Carl Burks provides articles, musings and insight into technology issues, software development, and other selected topics.

Counting calories with a few commands

2014-02-11T19:14:00.002-08:00

Authors:
Carl Burks
Lets say you like to use the Linux shell for totaling up your calories. Maybe you've got a file something like this:

590soft beef tacos
290fish sandwich
380fries

You could manually calculate these or... you could use some nifty Linux programs. First lets get those numbers away from the rest of the file.

cat 20140211.calories | cut -f 1

gives you:

590
290
380

cut uses as the default delimiter and we want the first field.

Now you've got the numbers but they are not in a usable math expression so you can use "paste"

cat 20140211.calories | cut -f 1 | paste -s -d+

gives you:

590+290+380

now you just need to total it all up with "bc"


ExplainShell.com can give a complete break down:

cut -f 1 | paste -s -d+ | bc