I have a problem with my awk script. It's main purpose is to take the sum of 2 following lines, calculate mean and than go to the next lines. I want my output to be one column of means.
The problem is when I have 0 (number) in the input. In such case awk somehow skips this line. So the output of this:
2
3
4
0
is this:
2.5
I want it to work like this:
input example:
2
3
6
7
output example:
2.5
6.5
My awk script looks like this:
BEGIN { x = 0}
$0 {
x = x + 1
sum += $0
if ( x == 2 ) {
print sum/x
x = 0
sum = 0
}
}
How can I make this work?
Thx
The problem is with using $0
alone as a condition. This will be false if the value is an empty string or the number 0
.
Change that to $0 != ""
so you just compare it with the string. Or use NF
to check if there is at least 1 field in the row.