Reputation: 91
Going through an awk tutorial, I came across this line
substr($0,20,5) == "HELLO" {print}
which prints a line if there is a "HELLO" string starting at 20th char.
Now I thought curly braces were necessary at the start of an awk script and an 'if' for this to work, but it works without nevertheless.
Can some explain how it evaluates?
Upvotes: 2
Views: 781
Reputation: 295687
If you have:
{ action }
...then that action runs on every line. By contrast, if you have:
condition { action }
...then that action runs only against lines for which the condition is true.
Finally, if you have only a condition, then the default action is print
:
NR % 2 == 0
...will thus print every other line.
You can similarly have multiple pairs in a single script:
condition1 { action1 }
condition2 { action2 }
{ unconditional_action }
...and can also have BEGIN
and END
blocks, which run at the start and end of execution.
Upvotes: 6