Quote:
Originally Posted by sarenace
Why does that work? Whats the difference between !~ and ~! ?
|
In awk,
~ is the regular expression match operator.
!~ is the regular expression no-match operator.
There is no
~! operator at all. You cannot modify a regular expression by prepending
! to it. It is not in any regular expression rules. In general, prepending
! in front of a regular expression is an error.
To awk, constant regular expressions like
/pattern/ are treated according to the same rules that apply to variables. In fact, if you use
/pattern/ where a regular expression is not expected (for example,
print /foo/), it evaluates to 0. (In awk, logical "false" is represented using 0, and "true" using 1 (or any nonzero value). Same rules ANSI C uses.)
When you specify
~ !/pattern/, the not operator (
!) applies to
/pattern/ first. Because the not operator has nothing to do with regular expressions, the
/pattern/ is evaluated as a variable, yielding 0. The not operator inverts that to 1. The result is the same as if you had written
~ 1. Because of the rules awk uses, the right side may be a variable or a string, and according to awk promotion rules, that
1 is converted to a string. The end result is just as if you had written
~ "1" or
~ /1/.
In other words, according to awk rules,
(something ~ !/pattern/) is the exact same thing as
(something ~ /1/) .