Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. 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.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

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.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

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:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Using awk to sum/count a column of numbers.

Terminal - Using awk to sum/count a column of numbers.
cat count.txt | awk '{ sum+=$1} END {print sum}'
2009-03-16 00:22:13
User: duxklr
Functions: awk cat
5
Using awk to sum/count a column of numbers.

Takes a input file (count.txt) that looks like:

1

2

3

4

5

It will add/sum the first column of numbers.

Alternatives

There are 2 alternatives - vote for the best!

Terminal - Alternatives
numsum count.txt
2010-01-26 21:53:18
User: deltaray
5

numsum is part of of the num-utils package, which is available in some Linux distros and can also be downloaded at http://suso.suso.org/xulu/Num-utils. It contains about 10 different programs for dealing with numbers from the command line.

Obviously you can do a lot of things that the num-utils programs do in awk, sed, bash, perl scripts, but num-utils are there so that you don't have to remember the syntax for more complex operations and can just think: compute the sum, average, boundary numbers, etc.

echo $(( $( cat count.txt | tr "\n" "+" | xargs -I{} echo {} 0 ) ))
2010-01-27 10:02:30
User: glaudiston
Functions: cat echo tr xargs
0

if you, like me, do not have the numsum, this way can do the same.

Know a better way?

If you can do better, submit your command here.

What others think

@unixmonkey2316 We love awk but save the cat's! :-)

< count.txt awk '{sum+=$1} END {print sum}'

Comment by mpb 150 weeks and 4 days ago

Or:

printf '%s\n' $(( $(paste<infile -sd+) ))
Comment by radoulov 150 weeks and 4 days ago
echo test | fold -w 1

t

e

s

t

Comment by OJM 150 weeks and 4 days ago

@myself: silly me, sorry for the noise, all wrong.

Comment by OJM 150 weeks and 4 days ago

How about:

awk '{ sum+=$1} END {print sum}' count.txt

Another useless use of cat.

Comment by rjc 150 weeks and 4 days ago

@rjc those who don't understand awk have redundancies all over the place. 'grep foo | awk', 'cat bar | awk', etc.

Comment by atoponce 150 weeks and 3 days ago

cat test.log | awk -F, '{sum+=$1} END{print sum}'

But my input file is

1,www.zzzzzzzzzzzzz/,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,bro.jpg&h=70&w=105&usg=AFrqEzeDZa4qbOTgz8UtMnCmRSVWz6Jccg","LG-KP500 Teleca/WAP2.0 MIDP-2.0/CLDC-1.1","","image/jpeg"

2,www.t.com

Iam getting error :

records number 1 has too many feilds...

Can you give me the solutions

Comment by Jay 73 weeks and 4 days ago

Your point of view

You must be signed in to comment.

Related sites and podcasts