Quantcast
Viewing all articles
Browse latest Browse all 13

Why can't a constant regular expression be put on the left side of an ~ operator in gawk?

Why cant I put a regular expression on the left side of the ~ operator when using gawk.

For example, given the file below with fields delimited with tabs(t):

$ cat cats
siberian    1970    73  2500
shorthair   1999    60  3000
longhair    1998    102 9859
scottish    2001    30  6000

If I use gawk to find a record, it works:

$ gawk '$1 ~ /h/' cats
shorthair   1999    60  3000
longhair    1998    102 9859
scottish    2001    30  6000

However if I move the operands $1 and /h/ around, it doesnt:

$ gawk '/h/ ~ $1' cats
gawk: cmd. line:1: warning: regular expression on left of `~' or `!~' operator

The gawk man page for the ~ operator says:

Regular expression match, negated match. NOTE: Do not use a constant
regular expression (/foo/) on the left-hand side of a ~ or !~. Only
use one on the right-hand side. The expression /foo/ ~ exp has the
same meaning as (($0 ~ /foo/) ~ exp). This is usually not what was
intended.

I dont understand how the expression /foo/ is evaluated to become ($0 ~ /foo/) and also this seems to only imply the weaker phrase “bad things will happen if you put a constant regular expression on the left” it doesnt actually imply the stronger phrase of “the behaviour of gawk is undefined if you put a constant regular expression on the left because it wasnt programmed to be used in this way”.

I basically dont understand how the operator ~ is evaluated internally.


Viewing all articles
Browse latest Browse all 13

Trending Articles