Index() function compares plain strings, not patterns.
The /…/ notation only works for patterns directly declared between the //. In addition, without an operator it makes a match against the whole of the current line.
If you want to check a field or a variable, you need to specify the ~ operator:
$3 ~ /foo.*bar/ OR myVar ~ /foo.*bar/
There are two ways to use a pattern that is contained in an awk string variable. (If I declare a pattern.RE in a variable, I name the variable reSomething, like:
reWord = “+$”;
Then the two available syntaxes are:
(a) someVar ~ reWord
That just returns a boolean result – 1 for match, 0 for not-match
(b) match (someVar, reWord)
That returns the index of the match, or zero (which is a useful boolen too).
In addition, it sets the global variables RSTART and RLENGTH which describes the substring of someVar which matched the pattern (0, -1 if no match).
It is less efficient to put a pattern in a variable than in //, because the // form is compiled in once at the start, but the variable is processed every time it is used.
However, I usually use a variable for complex patterns, because I can then give it a name that documents what it is for. Obviously, if the patterns is constructed from other stuff, then a variable is the only option you have.