venky513 venky513 - 6 months ago 106
Linux Question

what will be the output of the following command awk 'BEGIN{sum=0}END{print sum}’filename?

what will be the output of the following command?

awk 'BEGIN{sum=0}END{print sum}'filename

what will happen if we declared only the BEGIN and END blocks


First of all, you need to write a space before the filename, so that awk recognizes it as so:

awk 'BEGIN {sum=0} END {print sum}' filename
#                                  ^

This being said, this is not doing anything but printing 0:

$ cat file
$ awk 'BEGIN {sum=0} END {print sum}' file

Why is this happening? Well, before you are executing stuff before (in BEGIN) and after (in END) reading the file, but not during doing so. Then, since you are just setting a variable to 0 and then printing it, you can just expect the output to be 0.

awk composes on patterns and actions. This means that you need to perform some kind of action to have some content created. This is expressed by either of this:

[pattern]  { action }
 pattern  [{ action }]

So to perform a sum on all the lines in a file, you need to use some kind of action on the kind of {sum+=$0} which will keep a counter:

$ awk 'BEGIN {sum=0} {sum+=$0} END {print sum}' file

Still, we can skip the usage of BEGIN {sum=0} because any variable default to 0, so you don't need to set it:

$ awk '{sum+=$0} END {print sum}' file

And what if the file was empty?

$ rm file
$ touch file
$ awk '{sum+=$0} END {print sum}' file

You see this is not returning anything. To cover this case, use sum+0 so that the variable will be set on the fly when printing it:

$ awk '{sum+=$0} END {print sum+0}' file

I recommend you to read an awk manual, for example the GNU awk user's guide or Effective AWK, 4th edition by Arnold Robbins (hey Ed Morton I did suggest it first! ;) ).