{if ($2 == "M"){
s=s+$3
n=n+1
}END{
print s, n, s/n, "avg height"
}
hi
I am new to the forum and I am first time posting any reply.So if any thing is wrong forgive me fr that .
now the answer to the first question
My friend you have to first understand the basic rule for these lines and then you will understand the whole syntax easily . lets take it one by one.
if ($2 == "M")----- /* awk goes to every line of file 1 by 1 and executes the command repetedly fr every line so, $2 is compared with the 2nd field that is 'M' since its boolean output is true then it goes to next line */
{
s=s+$3 -----/* Note after the block i.e '{' you can assign a variable (here it is s & n) and if that variable is not initialized then in shellscript uninitialized variable is given the value 0 so your values fr the first line scanning are s=0+70 ===> s=70
n=0+1 ===> n=1
}
and again for the second time you get if ($2 == "M") =true then you are assign
s=s+$3 => s=70+69 = 139
n=n+1 => n=1+1 =2
now once all the lines are scannned in a file
then there is final action taken on the results obtained during scanning of file
this is done by
END{final action}
so you have now variables and now you can get them printed the way you like i.e
END{
print s, n, s/n, "avg height"
}
thanx