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. Show Sample Output
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) Show Sample Output
Read all lines using decimal marker as point, then add all them up and outputs the result. Show Sample Output
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. Show Sample Output
Same as the seq/bc solution but without bc.
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. Show Sample Output
http://en.wikipedia.org/wiki/Pascal%27s_triangle This one's for bartonski. Enjoy. 132 characters. I'm sure we can do better. Note: after row 64 we overflow integer maths. Show Sample Output
More readable version of factorial calculation using standard desktop calculator instead of 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. Show Sample Output
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.
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
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: