I'm learning AWK these days for text processing. But I'm very much confused about AWK syntax. I read on Wikipedia that the syntax follows this format:
(conditions) {actions}
I assumed I can follow the same syntax in a BEGIN and END block. But I'm getting a syntax error when I run the following script.
awk 'BEGIN{}
(1 == 1) {print "hello";}
END{
(1==1) {print "ended"}}' $1
However, if I make a little bit of a change inside the END block and add 'if' before the condition, it runs just fine.
awk 'BEGIN{}
(1 == 1) {print "hello";}
END{
if (1==1) {print "ended"}}' $1
Why is it mandatory to write 'if' in END block while it's not needed in normal blocks?
Best Answer
AWK programs are a series of rules, and possibly functions. Rules are defined as a pattern (
(conditions)
in your format) followed by an action; either are optional.BEGIN
andEND
are special patterns.Thus in
the patterns are
BEGIN
,(1 == 1)
(and the parentheses aren’t necessary), andEND
.Blocks inside braces after a pattern (or without a pattern, to match everything) are actions. You can’t write patterns as such inside a block, each block is governed by the pattern which introduces it. Conditions inside an action must be specified as part of an
if
statement (or other conditional statement,while
etc.).The actions above are
{}
(the empty action),{ print "hello"; }
, and{ if (1 == 1) { print "ended" } }
.A block consisting of
{ (1 == 1) { print "ended" } }
results in a syntax error because(1 == 1)
is a statement here, and must be separated from the following statement in some way;{ 1 == 1; { print "ended" } }
would be valid, but wouldn’t have the effect you want —1 == 1
will be evaluated, then separately,{ print "ended" }
.