lang@sirius.PRC.Unisys.COM (Francois-Michel Lang) (04/26/89)
An AWK question for the resident wizards: Is there any way to do pattern-matching on an AWK variable? E.g., if I want to do something every time I find a line containing the string "foo", I can say /foo/ { ... <ACTION> ... } Similarly, if I want to do something every time I find a line whose third word contains the string "foo", I can do $3 ~ /foo/ { ... <ACTION> ... } 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. ---------------------------------------------------------------------------- Francois-Michel Lang Paoli Research Center, Unisys Corporation lang@prc.unisys.com (215) 648-7256 Dept of Comp & Info Science, U of PA lang@cis.upenn.edu (215) 898-9511
dbk@mimsy.UUCP (Dan Kozak) (04/27/89)
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
steinbac@hpl-opus.HP.COM (Gunter Steinbach) (04/28/89)
I didn't look up any book passages, but I quickly tried the following in K-shell: nawk '{a="x"; if($1 ~ a) print "match"}' asd xyz asx And sure enough, the last two input lines produced a "match" output. So no problemo! However, I also tried the above with the "old" awk, and it produces a "syntax error near line line". So you need the AT&T toolbox "nawk". Hope this helps you -- Gunter Steinbach Hewlett-Packard Labs gunter@hpl-opus
fyl@ssc.UUCP (Phil Hughes) (04/29/89)
In article <10029@burdvax.PRC.Unisys.COM>, lang@sirius.PRC.Unisys.COM (Francois-Michel Lang) writes: > An AWK question for the resident wizards: > Is there any way to do pattern-matching on an AWK variable? You need nawk (the awk shipped with 5.3.2). You can say: BEGIN { junk = "hi" } $1 ~ junk { print "wow!" } and it does what you expect (and want) -- Phil Hughes, SSC, Inc. P.O. Box 55549, Seattle, WA 98155 (206)FOR-UNIX uw-beaver!tikal!ssc!fyl or uunet!pilchuck!ssc!fyl or attmail!ssc!fyl
jpr@dasys1.UUCP (Jean-Pierre Radley) (04/30/89)
In article <10029@burdvax.PRC.Unisys.COM> lang@sirius.PRC.Unisys.COM (Francois-Michel Lang) writes: >An AWK question for the resident wizards: >Is there any way to do pattern-matching on an AWK variable? Yes, why not? >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. Awk variables are valid anywhere in an awk script. Maybe I'll just give you an example excerpted from some code of mine: awk ' ... $4!=Num { ... } { AccT+=$6; GT+=$6; ST+=$6; Num=$4 ... } ... ' At some point I set the variable Num equal to something. At some other point, I test it. Just do it. -- Jean-Pierre Radley Honi soit jpr@dasys1.UUCP New York, New York qui mal ...!hombre!jpradley!jpr CIS: 76120,1341 y pense ...!hombre!trigere!jpr