Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!tank!mimsy!dbk From: dbk@mimsy.UUCP (Dan Kozak) Newsgroups: comp.unix.questions Subject: Re: variables in awk Summary: dynamic regular expressions, nawk Keywords: awk, nawk Message-ID: <17138@mimsy.UUCP> Date: 27 Apr 89 05:43:09 GMT References: <10029@burdvax.PRC.Unisys.COM> Reply-To: dbk@mimsy.umd.edu.UUCP (Dan Kozak) Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 44 In article <10029@burdvax.PRC.Unisys.COM> lang@sirius.PRC.Unisys.COM (Francois-Michel Lang) writes: >Is there any way to do pattern-matching on an AWK variable? >Now, the question is, what if I want to match not against >a given string (which is fixed at "compile time"), >but against an AWK variable, whose value can change >in the course of the life of the AWK script. >I can't find anything in any AWK documentation >that tells me that this is possible, and, if so, >how to do it. Any pointers would be appreciated. This is one of the additions to nawk (new awk) and is called dynamic regular expressions. The slashes around the usual kind of regular expression, i.e. /foo/, indicate that it is a constant. So you get rid of those and put the variable's name in the same place. Here's an (admitedly contrived) example: BEGIN { ss = "dbk" } $1 ~ ss { print; ss = "jrl"; } When run like this: who | nawk -f awk.tst it lists the first occurance of my logon in the who listing and any subsequent occurances of jrl. Mind you, there is one difference between this and constant regular expressions that I've found (bug or feature? YOU be the judge!). Although you can specify a pattern like: /jrl/ { print $3; } you have to this with a dynamic regex: foo = "jrl" $0 ~ foo { print $3; } i.e. you have to make the match explict. Happy nawking! #dan dbk@mimsy.umd.edu uunet!mimsy!dbk