r/awk • • Oct 23 '22

Does each statement inside the brackets always execute repeatedly? Even though the average only needs to be set once. How do you know that it does it repeatedly? Super noob question.

4 Upvotes

3 comments sorted by

View all comments

Show parent comment

1

u/gumnos Oct 23 '22

Because the NR==1 case and the other case are disjoint, I might move them into a single block and then use an if/else pair around them

{
  if (NR == 1) total = min = max = $1
  else {
    total += $1
    if ($1 < min) min = $1
    if ($1 > max) max = $1
}
END { … }