commandlinefu.com is the place to record those command-line gems that you return to again and again.
You can sign-in using OpenID credentials, or register a traditional username and password.
Subscribe to the feed for:
More readable version of factorial calculation using standard desktop calculator instead of bc.
Same as the seq/bc solution but without bc.
I can't put the last ^2 with seq, so I reverse it to delete the last +N. So for doing sum(N^2) you have to do sum((N+1)^2). Must be a better way.
Calculate pi from the infinite series 4/1 - 4/3 + 4/5 - 4/7 + ...
This expansion was formulated by Gottfried Leibniz: http://en.wikipedia.org/wiki/Leibniz_formula_for_pi
I helped rubenmoran create the sum of a sequence of numbers and he replied with a command for the sequence: 1 + 2 -3 + 4 ...
This set me thinking. Transcendental numbers!
seq provides the odd numbers 1, 3, 5
sed turns them into 4/1 4/3 4/5
paste inserts - and +
bc -l does the calculation
Note: 100 million iterations takes quite a while. 1 billion and I run out of memory.
To do hex to binary: echo 'ibase=16; obase=2; 16*16' | bc # prints: 111100100
To do 16*16 from decimal to hex: echo 'ibase=10; obase=16; 16*16' | bc # prints: 100
You get the idea... Alternatively, run bc in interactive mode (see man page)
In this example, file contains five columns where first column is text. Variance is calculated for columns 2 - 5 by using perl module Statistics::Descriptive. There are many more statistical functions available in the module.
expr will give you a quick way to do basic math from the CLI. Make sure you escape things like * and leave a space between operators and digits.