The following AWK format:
/REGEX/ {Action}
Will execute Action
if the current line matches REGEX
.
Is there a way to add an else
clause, which will be executed if the current line does not matches the regex, without using if-then-else explicitly, something like:
/REGEX/ {Action-if-matches} {Action-if-does-not-match}
Not so short:
/REGEX/ {Action-if-matches}
! /REGEX/ {Action-if-does-not-match}
But (g)awk supports the ternary operator too:
{ /REGEX/ ? matching=1 : matching = 0 ; if ( matching ==1 ) { matching_action } else { notmatching_action } }
UPDATE:
According to the great Glenn Jackman you can assign variables on the match like:
m = /REGEX/ { matching-action } !m { NOT-matching-action }