🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.misc
Article #1279

Using variable for awk regexs

#1279
From: Lee F Kolakowski
Date: Sat, 09 Jun 1990 19:46
54 lines
2203 bytes
I have a samll problem in awk (gawk specifically), and I hope this is
the right place for this posting (I couldn't find a comp.lang.awk,
maybe there should be one :-) ).

I have a datafile that looks like this

id	regex
PS0TEST	^MAQQWSLQRLA
PS1TEST	^M
PS2TEST	MAQQWSLQRLA$
PS00001	N[^P][ST][^P]
PS00002	SG[A-Z]G

I read this info into 2 arrays (id[], and regex[]).

All is fine up to this point.

then I want to loop through all the regexs and search each against 
a string

    if ($0 ~ /^M/) 
      printf("matches pattern '%s'\n", "test")

    for (i = 1; i <= regex_num ; i++ ) {
      if ($0 ~ regex[i]) {
	printf("matches pattern '%s'\n", id[i])
    }

So the first case works as is should (the pattern /^M/)
But the loop does not match anything. This appears to be
because the variable regex[i] is not being expanded
before the comparison, so every time $0 is being 
compared to the string regex[i].

Is there a work around for this. I can't seem to find any
mention of this sort of thing in "The AWK Programming Language".



--

Frank Kolakowski 

======================================================================
|lfk@hx.lcs.mit.edu                     ||      Lee F. Kolakowski    |
|kolakowski@tropicana.mit.edu           ||	M.I.T.		     |
|lfk@mbio.med.upenn.edu		        ||	Dept of Chemistry    |
|AT&T:  1-617-253-1866                  ||	Room 18-506	     |
|#include <litigate.h>         		||	77 Massachusetts Ave.|
|                                    	||	Cambridge, MA 02139  |
|--------------------------------------------------------------------|
|		           One-Liner Here!                           |
======================================================================
Technical Details
Message-ID: <1990Jun9.164748.19880@athena.mit.edu>
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!aplcen!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!usc!snorkelwacker!bloom-beacon!bloom-beacon!lfk

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!aplcen!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!usc!snorkelwacker!bloom-beacon!bloom-beacon!lfk
From: lfk@E40-008-6.athena.mit.edu (Lee F Kolakowski)
Newsgroups: comp.lang.misc
Subject: Using variable for awk regexs
Message-ID: <1990Jun9.164748.19880@athena.mit.edu>
Date: 9 Jun 90 19:46:52 GMT
Sender: news@athena.mit.edu (News system)
Organization: Mass. Inst. of Tech., Dept. of Chemistry
Lines: 54