Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!cmcl2!beta!dph From: dph@beta.UUCP (David P Huelsbeck) Newsgroups: comp.unix.questions Subject: Re: An awk question or two... Message-ID: <10025@beta.UUCP> Date: Mon, 14-Sep-87 12:16:04 EDT Article-I.D.: beta.10025 Posted: Mon Sep 14 12:16:04 1987 Date-Received: Tue, 15-Sep-87 05:55:22 EDT References: <3931@well.UUCP> <27817@sun.uucp> Reply-To: dph@LANL.GOV.ARPA (David P Huelsbeck) Distribution: na Organization: Los Alamos Natl Lab, Los Alamos, N.M. Lines: 74 Keywords: awk ranges Summary: You can at least do what you want In article <27817@sun.uucp> guy@sun.uucp (Guy Harris) writes: >In article <3931@well.UUCP> daniels@well.UUCP (Dan Smith, Social Mammal...) > writes: >> awk -f comline.awk comvar=\"SUB\" ascii.h >> >> this is the file comline.awk: >> >> comvar { print } >> >> and, the result: >> [ usual wonderfully helpful awk error messages deleted ] > >"comvar" is not a legal pattern. A pattern is either a keyword (such as BEGIN >or END), a relational expression, or a regular expression. You can't just >throw a variable name there and expect "awk" to treat that as a regular >expression that matches all lines that contain that regular expression; "awk" >doesn't permit that. TRUE. >What you want to do instead is to pass the program to "awk" *on the command >line*, and put it in double quotes so that shell variables are expanded before >the string is passed to "awk". Maybe, maybe not. > Guy Harris Guy is correct. But if you'll look at my spreadsheet calculator example you'll see that in this specific case there is a way to do what you want to do. I haven't yet figured out a way to do this using real regexs and pattern matching yet but I'll bet if you use a sufficiently twisted method you could do it somehow. After all that's the fun of awk. ;-) Try this: #comline.awk (script to do something) $1 == arg1 , $1 == arg2 { if ($1 ~ /-f/) print $2 } Then awk -f comline.awk arg1="-ds" arg2="-de" ascii.h and you get: bhuff.bin bhuff.c dirinfo The important thing to note here is that whenever you use ~ or !~ one of the operands must be a true pattern i.e. /regex/ This is too bad but I think it is a result of the fact that awk "compiles" regexs during the script compilation phase. I suppose it does this for reasons of speed but I'm not sure enough to say. So while putting $1 == xyz in the pattern field is OK $1 ~ xyz will cause awk to barf. This is true everywhere. So "if ($1 ~ "-f") print $2 " will net you a syntax error also. This is the best AWK solution I could come up with. If you need something more using the shell variables is the best way I can think of just now. If you can figure out a way around this limitation without using the shell I'd like to see it. David Huelsbeck dph@LANL.GOV {cmcl2,ihnp4}!lanl!dph #include