I have a file with entries seperated by an empty space. For example:
example.txt
24676 256 218503341 2173
13236272 500 1023073758 5089
2230304 96 15622969 705
0 22 0 526
13277 28 379182 141
I would like to print, in the command line, the outcome of “column 1/ column 3” or simila. I believe it can be done with awk. However, some entries are 0, hence division by 0 gives:
fatal: division by zero attempted
In a more advanced case, I would like to find the median value (or some percentile) of the division.
There are many ways to ignore the row with a zero divisor, including:
The question changed — to print 0 instead. The answer is not much harder:
Medians and other percentiles are much fiddlier to deal with. It’s easiest if the data is in sorted order. So much easier that I’d expect to use a numeric sort and then process the data from there.
I dug out an old shell script which computes descriptive statistics – min, max, mode, median, and deciles of a single numeric column of data:
The initial values of
minandmaxare not exactly scientific. It serves to illustrate a point.(This 1997 version is almost identical to its 1991 predecessor – all except for the version information line is identical, in fact. So, the code is over 20 years old.)