As a one-liner this awk example is rather complicated.
{ if (A!=$1) { # This section has a different A-column if (a) { # If a>0, then it is not the beginning print A,b/a # Print result } A=$1; # Re-init variables a=0; b=0 } ++a; b += $2 ? 1 : 0 }
To run this, put the awk script in frac-calc and the numbers in number and run it:
( cat number; echo ) | awk -E frac-calc
The output would be:
494 0.5 500 0.333333 501 0.25
The reason why the echo is needed, is that it ensure the result of the last block (501) to be printed, as column A is different.
It can also be a long one-liner:
( cat number; echo ) | awk '{if(A!=$1){if(a){print A,b/a}A=$1;a=0;b=0}++a;b+=$2?1:0}'
Edit: With the use of END and without echo as mentioned in the comments:
{ if (A!=$1) { # This section has a different A-column if (a) { # If a>0, then it is not the beginning print A,b/a # Print result } A=$1; # Re-init variables a=0; b=0 } ++a; b += $2 ? 1 : 0 } END { print A,b/a # Print result }
And call it:
awk -E frac-calc number
The one liner is then a bit longer:
awk '{if(A!=$1){if(a){print A,b/a}A=$1;a=0;b=0}++a;b+=$2?1:0}END{print A,b/a}' number
1or blank? Please do not respond in comments; edit your question to make it clearer and more complete.