# Calculating series with awk: add numbers from 1 to 100

awk 'BEGIN {for(i=1;i<=100;i++)sum+=i}; END {print sum}' /dev/null
Calculating series with awk only, no need for seq: add numbers from 1 to 100 Variations: 1+3+...+(2n-1) = n^2 `awk 'BEGIN {for(i=1;i<=19;i+=2)sum+=i}; END {print sum}' /dev/null # displays 100` 1/2 + 1/4 + ... = 1 `awk 'BEGIN {for(i=1;i<=10;i++)sum+=1/(2**i)}; END {print sum}' /dev/null # displays 0.999023`
Sample Output
`5050`

0
2009-10-26 18:24:57

## 1 Alternatives + Submit Alt

• "seq 100" outputs 1,2,..,100, separated by newlines. awk adds them up and displays the sum. "seq 1 2 11" outputs 1,3,..,11. Variations: 1+3+...+(2n-1) = n^2 `seq 1 2 19 | awk '{sum+=\$1} END {print sum}' # displays 100` 1/2 + 1/4 + ... = 1 `seq 10 | awk '{sum+=1/(2**\$1)} END {print sum}' # displays 0.999023` Show Sample Output

4
seq 100 | awk '{sum+=\$1} END {print sum}'
· 2009-03-24 20:30:40

### 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?

commandlinefu.com 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.

### Stay in the loop…  