10

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?

0
25

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 and END are special patterns.

Thus in

BEGIN {}
(1 == 1) { print "hello"; }
END { if (1 == 1) { print "ended" } }

the patterns are BEGIN, (1 == 1) (and the parentheses aren’t necessary), and END.

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" }.

0
2

Quoting (english) Wikipedia - Structure of AWK programs

An AWK program is a series of pattern action pairs, written as:

condition { action }
condition { action }
...

where condition is typically an expression and action is a series of commands.

What the article fails to mention is, the condition or pattern is optional. This means in your case (1 == 1) is not needed, because this is always true.


Rewriting the snippet yields

BEGIN {}
{ print "hello"; }
END { print "ended"; }

And having an empty action, you can skip the line altogether

{ print "hello"; }
END { print "ended"; }

Also note that in the example

END {(1 == 1) { print "ended"; }}

The pattern/condition is END and not (1 == 1). 1 == 1 is inside the action, and so is part of the action. This is also the reason, why there's an error.

1
  • 1
    Actually I was just trying to learn this conditional statement syntax. So instead of writing some effective conditions like $1 >5, I was just putting (1 == 1) and (1 != 1 ) for simulating true and false condition . My whole thought process was if the syntax works fine, I can simple replace this 1 == 1 with some useful conditions like $1 == 22 or anything else which is required for a given problem. I was unclear why the exact same statement didnt work in END block. As explained by your answer ( and @Stephen kitt's), I got to know END is actually a special pattern. – Abinash Dash Feb 27 at 4:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.