4 ms·
By the way, Awk is whitespace sensitive: pattern { action } # valid pattern { # valid action } pattern # not what you
by kazinator 7y ago
By the way, Awk is whitespace sensitive:
pattern { action } # valid
pattern { # valid
action
}
pattern # not what you think
{ action } # this action has no pattern
There cannot be a newline between pattern an action. This feature allows either the pattern or the action to be omitted without ambiguity.
pattern # pattern with default { print } action
{ action } # unconditional action
pattern { # pattern with action
action
}
Items can be put on a single line without ambiguity using semicolons:
pattern ; { action } ; pattern { action }
A pattern can have multiple patterns separated by a comma. That syntax admits optional line separation after the comma separators:
pattern,
pattern,
pattern {
action
}
The action fires by a match for any of the patterns.
The POSIX standard Awk expression grammar has no comma operator, on the other hand; the comma exists only for separating patterns, and function arguments/parameters, and in the print statement syntax.
Plus, JNIL (just now I learned). The in operator allows comma-separated expressions for testing "multi-dimensional" array membership. Here is a hello, world:
$ awk 'BEGIN { a[1,2,3] = 4 ; print (1,2,3) in a }'
1
(Note that multi-dimensional arrays in Awk are simulated; a string index is generated for that 1,2,3).