Steffen Schuler wrote:
> On Mon, 18 Feb 2008 04:54:11 -0800, Spiros Bousbouras wrote:
>
>
>>On page 22 of "Effective AWK programming" we read:
>>
>> When awk statements within one rule are short, you might want to
>> put more than one of them on a line. This is accomplished by
>> separating the statements with a semicolon (`;'). This also
>> applies to the rules themselves. Thus, the program shown at the
>> start of this section could also be written this way:
>> /12/ { print $0 } ; /21/ { print $0 }
>> NOTE: The requirement that states that rules on the same line
>> must be separated with a semicolon was not in the original awk
>> language; it was added for consistency with the treatment of
>> statements within an action.
>>
>>But when I try
>>gawk --posix '/a/ {print "swds"} /b/ {print "1234"}' it works fine.
>>Same if I omit "--posix". If it is a requirement then why does omitting
>>semicolons work ?
>
>
> If you have two rules consisting only of patterns like /a/ and /b/ or
> NR == 1 and NR == 10 and put both conditions (rules) on a line you need
to
> separate them by semicolon; otherwise you get normally an error message
> or a strange behavior.
Your syntactical reasoning is correct.
Mind that a rule of NR==1 ; NR==10 may be written as NR==1 || NR==10
while /a/ ; /b/ would print any line _two times_ which happen to
match both patterns (that might or might not be what you want; in case
of the default action in above examples I would suspect that often a
unique print $0 response is more likely to be what's needed - YMMV).
So /a/ || /b/ *might* be the more appropriate construct. Anyway...
Given that the expression /a/ ; /b/ obfuscates the implicit double
print $0 action I'd prefer to write it on two lines, at least. Another
reason to put that patterns on separate lines is a possible confusion
with the more common range pattern /a/,/b/ which uses a glyph similar
to /a/;/b/ .
WRT the original question; I'd omit any superfluous semicolon in between
two condition/action blocks. And I cannot see why that was introduced,
if you compare the awk syntax to the C language; there's the possibility
to write an (possibly empty) block *or* a semicolon, but not both, where
a command/block is required (e.g. if(c);else{} ).
Janis


|