Sum some columns on one line in a csv file.

perl -ne '@a=split(/,/); $b=0; foreach $r (1..$#a){ $b+=$a[$r] } print "$a[0],$b\n"' -f file.csv
For all lines, sum the columns following the first one, and then print the first column plus the sum of all the other columns. example input: 1,2,3,4,5 2,2,3,4,5 becomes 1,14 2,14

By: miniker84
2015-09-04 21:05:56

2 Alternatives + Submit Alt

What Others Think

Looks nice, but as always some sample data and output will make it better.
flatcap · 325 weeks and 1 day ago
I do not think we should be promoting `perl -ne`-type snippets, unless there is absolutely no other way. It is better than using some other language, but I think in this case sed and bc can do it. Simply replace all , with + and pass to bc. sed -e 's/\(\s\+\)\?,\(\s\+\)\?/+/g' < file.csv | bc If you have GNU sed, you can use -r to make this much easier to read: sed -r -e 's/(\s+)?,(\s+)?/+/g' < file.csv | bc Yes, this works for floating point too. file.csv: 1,2,3,4,5.5 7,2,3.6666,5,10 Output: 15.5 27.6666
Tatsh · 325 weeks and 1 day ago
Hmm... Now I've bothered to try out the command, it doesn't do what I thought it did. i.e. it doesn't match the description. . It ignores the first column, then sums the other columns: 1,2,3,4,5 becomes 1,14 . @tatsh: Your command just sums the rows, it doesn't match the original command. . @tatsh: I disagree with your comment about perl. OK, it's a bit high-level, but I'm always keen to see as many possible alternatives for these "computational" types of commands. Personally, I too would reach for sed or awk first.
flatcap · 325 weeks ago
miniker84 · 324 weeks and 6 days ago
@flatcap Then we are not in disagreement. I prefer to see 'simpler' languages (sed, awk) before getting into `| some_language_interpreter -e` type commands. (However, one should not use grep -P (GNU extension for PCRE) over Perl.) The goal here seems to be to keep things short, but not obtuse. I think it is easier to achieve that goal (when it is appropriate) by sticking to the base coreutils. Perl is nice for things like: perl -e 'for($t=0;;$t++){print chr +($t*($t^$t+($t>>15|1)^($t-1280^$t)>>10))%256}' | play -t s8 -v 0.25 - (play is a symlink to sox; it needs to be invoked that way, this should come with your installation of sox) Yes this could be (very similarly) done in awk but few could read it in one line very easily by comparison.
Tatsh · 324 weeks and 6 days ago
scream, or enjoy the cicadas flap its wings when you bring the cool wind; when passing Hasuike also puffed breath goods products that Lotus aroma; hours because ignorance has been poked hornet's nest and stung his face was bruised. Dig once, because "small insects" (Sparrow) and took out a "snake" (Snake) scared to fall from the wall. Fortunately, nothing serious. Leisure time, will collect some cigarette paper folded triangle, and partners to play; (the triangle on the ground and put his hand on the ground quick photo shoot the other side of the triangle is the triangle that turned their a); and also than own older brother and sister were led to a wide flat on the grass or doing "of chicken" or "kill the lamb" of the game, before the election when one eagle, then tall in front when hens , when the rest of the chicken, the eagle chasing shot at the back of the chick, hen with open arms to protect chickens, smart dodge to keep eagle patting. Or a dozen people sitting in a circle, do a do Diushou Juan game: the hands of only one person to take the handkerchief out of his mouth in a circle singing Diushou Juan songs, danced around the circle turning quietly throw the handkerchief in the back of a person. Then run around in circles. If you have a lap behind the handkerchief patting, this people have not found the person you want to give a performance for everyone; a joke, sing songs, or back poem. Then began Diushou Juan by him or her. If this is discovered immediately took the handkerchief catch Diushou Juan people, that Diushou Juan will continue to lose, someone who can back into place; catch, and throw this man who has sat on the bit when this man will be punished performances. We will play along with a dozen "Stars over the month child" or "cat drilled 72 holes" game. (Read the article Network: I remember one summer, that the acquisition of medicinal city recovered cicada skin (medical term called cicada), catty two five. So every day, together with my brothers holding bamboo, Dianzhe plastic bags, and from the village in the woods to find a tree cicada skin, after the end of the summer, we harvest relatively big, full harvest four pounds, to buy ten dollars. The next summer we look for a summer, more than the first year looking for even more, I did not expect there to happily after being told not recovered. Cicada got hard skin, back at home after shuffling, and
zyf543 · 294 weeks and 1 day ago
I love how there are alternative codes to try. I will try this LA Auto Glass Repair on to see if it really helps.
TylerCorona · 12 weeks and 5 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: