Hi,
I have 3 columns in a file listed below.
X Y X/(X+Y)
1 1 0.5
1 1 0.5
4 1 0.8
1 1 0.5
6 1 0.857142857
1 1 0.5
23 1 0.958333333
Now I want to find confidence interval using the formula for each row.
(p-2 sqrt p(1-p)/(x+y), p+2 sqrt p(1-p)/(x+y)) =(a,b)
where p = x/(x+y) already calculated in column 3 of the file..
I want the values(a,b) to be placed in column 4.
Thanks,
Diya
nawk '{a=$3-2*sqrt($3*(1-$3)/($1+$2));b=$3+2 * sqrt($3*(1-$3)/($1+$2)); print $0, a","b}' myFile
Hi,
Thanks for the reply,
Firstly I cannot run using nawk as it says command not found.Probably I have to install it on my Linux machine.
When I ran the command with awk it generated me the following error.
awk '{a=$3-2*sqrt($3*(1-$3)/($1+$2));b=$3+2 * sqrt($3*(1-$3)/($1+$2)); print $0, a","b}' confi.txt
awk: (FILENAME=confi.txt FNR=1) fatal: division by zero attempted
Thanks,
given myFile:
1 1 0.5
1 1 0.5
4 1 0.8
1 1 0.5
6 1 0.857142857
1 1 0.5
23 1 0.958333333
the following:
nawk '{a=$3-2*sqrt($3*(1-$3)/($1+$2));b=$3+2 * sqrt($3*(1-$3)/($1+$2)); print $0, a","b}' myFile
produces:
1 1 0.5 -0.207107,1.20711
1 1 0.5 -0.207107,1.20711
4 1 0.8 0.442229,1.15777
1 1 0.5 -0.207107,1.20711
6 1 0.857142857 0.592623,1.12166
1 1 0.5 -0.207107,1.20711
23 1 0.958333333 0.876755,1.03991
What's your data file look like?
You probably need to cover the case where '$1+$2' results in '0'
Thanks.. It worked.. I was giving the header line so it did not work..
You might want to make the precedences of operators a little bit more explicit - based on YOUR understanding of the formulae:
a=($3-2)*sqrt($3*(1-$3)/($1+$2))
OR
a=$3-(2*sqrt($3*(1-$3)/($1+$2)))
and the same goes for the 'sqrt' and the others.....
Having Linux means you have GNU awk, meaning nawk isn't necessary. This is why it's always a good idea to say what your OS is when you ask a question...
Hi,
I have a file
ABC 1001 1500
ABC 1200 1900
CDE 1700 2200
DEF 2100 3200
IJK 4100 4400
IJK 4500 4800
I want the file in such a way that if the name is same in column 1 (ABC) then it has to pick the longest length (column C -B) and output the file
o/p
ABC 1200 1900
CDE 1700 2200
DEF 2100 3200
IJK 4500 4800
How can i do it in unix or awk?
Thanks,
Diya