Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a file that contains tab delimited decimals between 0 and 1, many rows X many-columns.
Does anyone know if/how it would be possible to manipulate each value by -log(X). I know I can do this using excel, but my dataset is huge so it would be much easier to do it with one of two unix commands.
Good suggestion. awk would be my choice, too. Just pay attention to the fact that in awk the log function gives the natural logarithm, whereas the OP asks for the base-10 logarithm, as clearly shown by the posted example. Hence you have to divide the natural logarithm by log(10):
I like the above suggestions too, but if for whatever reason they don't appeal to the OP, have a look at the bc man page. bc is the "basic calculator" and does log functions too.
Ok, I realize my awk suggestion is not of much use since it uses base e log. Also it does not address reading and writing files like you requested.
So, to make amends here is a little python script that should do what you want. It's not really unix, but, meh.
Code:
#!/usr/bin/env python
import sys
import math
from math import log
for line in sys.stdin:
for word in line.split():
try:
print -math.log10(float(word)),
except:
print word,
print
This should work even if there are things in the file that aren't numbers.
Assuming you call the script logall
Usage:
Code:
logall < infile > outfile
I'm sure perl people could do this in one line.
Cheers,
Evo2.
Last edited by evo2; 12-12-2009 at 06:05 PM.
Reason: Correct code tags
Edit: if you want to match exactly the number of digits in the floating point numbers, as shown in the example output, you can change the value of the CONVFMT built-in variable
Edit: if you want to match exactly the number of digits in the floating point numbers, as shown in the example output, you can change the value of the CONVFMT built-in variable
Id like to be able to limit the number of digits after the decimal. The floating point in the above command limits the number of digits after the last 0.
Ex: 0.00000000000009876 can be returned if the CONVFMT="%.4g"
I'm not familiar with the perl & python, so while there's likely a way to get the math functions to limit the output places, I don't know it. Meanwhile, what you *could* do if you simply want to chop the end off the decimals, is use sed:
Code:
echo 0.00000000000009876 | sed 's/\(.*\.[0-9][0-9][0-9][0-9]\)\(.*\)/\1/'
..just pipe the output of your formula stuff into the sed statement. NOTE: this doesn't round anything or do anything mathematical; it just chops the string down to 4 places-- that's all.
If you understand what %.9g you can easily find the answer. It is the notation of printf format used in C and inherited by many scripting languages or specific commands. Section 4.5.2 in the current version of the GAWK manual explains it all. Moreover you will find some detailed explanation and caveats about the usage of the CONVFMT internal variable. In the meanwhile you can give a try to CONVFMT=%.4f and see if it suits your requirement.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.