awk: "default" action if no pattern was matched? -
i have awk script checks lot of possible patterns, doing each pattern. want done in case none of patterns matched. i.e. this:
/pattern 1/ {action 1} /pattern 2/ {action 2} ... /pattern n/ {action n} default {default action}
where of course, "default" line no awk syntax , wish know if there such syntax (like there in swtich/case statements in many programming languages).
of course, can add "next" command after each action, tedious in case have many actions, , more importantly, prevents me matching line 2 or more patterns.
you invert match using negation operator !
like:
!/pattern 1|pattern 2|pattern/{default action}
but that's pretty nasty n>2
. alternatively use flag:
{f=0} /pattern 1/ {action 1;f=1} /pattern 2/ {action 2;f=1} ... /pattern n/ {action n;f=1} f==0{default action}
Comments
Post a Comment