awk statements follow the rule:
PATTERN{action}
So BEGIN
or END
are just special PATTERNs. basically if PATTERN is true, do the action in {..}
PATTERN could be regex, expression and range also empty.
the empty pattern looks like:
awk '{print "foo"}' input
You can read http://www.gnu.org/software/gawk/manual/gawk.html section 7.1 for deatails.
Back to your question, if you executed those 4 lines in Action part, that is, between {...}
, (in fact empty pattern), only the first, with if
is valid conditional statement. However if you use the matching check as pattern, (outside the {..}
):
if ($0 ~ /no/) {cmd} # this would ***NOT*** work, because it is statment, not valid awk expression. (I don't know how did you make it work.)
($0 ~ /no/) {cmd} # this will work, it is a boolean expression
$0 ~ /no/ {cmd} # same as above
/no/ {cmd} # this is regexp pattern, so it works too.