This is a test version of Biostars. For the public version, visit https://www.biostars.org.
awk for interval midpoints and length

Kind of a basic awk question but just trying to take the following intervals:

chr1 10001 10150
chr1 10008 10090
chr1 10025 10207
chr1 10058 10172

And generate a file output with a single command that in the first column has the midpoint of each and in the second column next to it has the entire interval length, such as this:

10075 149
10049 82
10116 182
10115 114

Thanks.

awk

Are we sure your expected output is correct, doesn't seem to match with cpad's answer.

Mean calculations in expected output are incorrect. It seems OP pasted exactly half of the expected range values to the last two numbers in mean column @ zx8754

1 answer

$ awk '{print int(($2+$3)/2), $3-$2}' test.txt
10075 149
10049 82
10116 182
10115 114

As mentioned above, it does not seem to work for some lines. I tested also on some other intervals.

Could you please post expected mean for non-working rows (records)? @ rbronste

Apologize, error on my part, copied incorrect output lines initially. Thanks.

Log in to answer this question.