I come across that awk
can be used to find the mean of the column as in here. Let's say I would like to compute the average of the second column, but only elements between specific rows (for example, from rows 2 up to 6). How to do this?
Asked
Active
Viewed 64 times
0
-
1Read [ask] and then [edit] your question to provide the missing info. – Ed Morton Sep 26 '16 at 17:34
-
@EdMorton: I went through the "How to ask" section. But I do not exactly see what is the missing information. Any specific pointers? – SKPS Sep 27 '16 at 07:52
3 Answers
2
$ awk '$2>=2 && $2<=6 {sum+=$2; count++}
END {print (count?sum/count:"N/A")}' file

karakfa
- 66,216
- 7
- 41
- 56
-
This does not work for data in scientific format. For example, +5.68902179E+00 – SKPS Sep 27 '16 at 08:35
-
I realized that my question lacked some clarity. I made the edit. What I meant is accessing columns between specific row intervals and not columns between specific values. – SKPS Sep 27 '16 at 09:07
-
0
Try:
$ awk -v s=2 -v e=6 '{if(($2>=s)&&($2<=e)){sum+=$2;n++}}END{print (n!=0)?sum/n:0}' input.txt
Explanation:
#!/bin/awk
BEGIN {
start = 2
end = 6
}
{
if( ($2 >= start) && ($2 <= end) )
{
sum = sum + $2
n = n + 1
}
}
END {
print ( n!= 0) ? sum / n : 0
}
#eof#
Hope it helps!

Lacobus
- 1,590
- 12
- 20
-
1`NR` won't be the number of records summed up! You have to use the actual count for mean computation. – karakfa Sep 26 '16 at 17:57
-
1Now you have to handle `n==0` case, otherwise you'll get `fatal: division by zero attempted` error. – karakfa Sep 26 '16 at 18:06
-
Writing `{ if (condition) action }` in awk is like writing `if (true) { if (condition) action } }` in C. Hopefully you wouldn't write that in C so don't write the equivalent in awk, it's just `condition { action }`. By the time you fix the issues with your answer, though, it'll be the same as @karakfa's answer. – Ed Morton Sep 26 '16 at 18:11