jaff@dhw68k.cts.com (Mark Jaffe) (05/25/89)
Awk seems to be lacking something, but maybe it's really there. I want to match a pattern that I don't know until I execute the script, so I want to pass in a pattern to match in a variable. I can't get this to work! Here's what I'm doing, on a Sun: . . SYSVER=`awk '{print $3}' < /etc/motd` . awk ' $0 ~ $SYSVER { print substr($0, 3, length - 2) } ' < cs35if.h > cs35.if . (in a shell script) Any suggestions? Mark Jaffe Transitional Technology, Inc. | jaff@dhw68k Orange, CA (714) 744-1030
poser@csli.Stanford.EDU (Bill Poser) (05/27/89)
In article <23206@dhw68k.cts.com>, jaff@dhw68k.cts.com (Mark Jaffe) writes: > Awk seems to be lacking something, but maybe it's really there. I want to > match a pattern that I don't know until I execute the script, so I want to > pass in a pattern to match in a variable. I can't get this to work! Here's > what I'm doing, on a Sun: > . > . > SYSVER=`awk '{print $3}' < /etc/motd` > . > awk ' $0 ~ $SYSVER { print substr($0, 3, length - 2) } ' < cs35if.h > cs35.if > . The problem is not with AWK, it is with the way you are quoting the AWK program. Single quotes in the shell prevent evaluation of variables. So $SYSVER is the pattern that AWK tries to match, not the VALUE of SYSVER. To get around this, you need to change the single quotes around the AWK program to double quotes. However, the shell will now evaluate the $0 that is intended to be evaluated by AWK. To prevent this, quote the $ with a backslash so that the shell will leave it alone. A simple example of all of this (that I know to work) is the following, which is my address book lookup command: awk " BEGIN {RS = \"\"} /$1/ {printf(\"%s\\n\\n\",\$0)}" < $HOME/misc/add The $1 is the command line argument to the shell script. The $0 is the "line" (actually a block of text due to the resetting of RS) read by AWK. Bill Poser
stefan@yendor.phx.mcd.mot.com (Stefan Loesch) (06/01/89)
In article <23206@dhw68k.cts.com> jaff@dhw68k.cts.com (Mark Jaffe) writes: >Awk seems to be lacking something, but maybe it's really there. I want to >match a pattern that I don't know until I execute the script, so I want to >pass in a pattern to match in a variable. I can't get this to work! Here's I ran into this problem before (years ago). At that time I was firm in the shell AND in awk :-), and played arround a couple of days. As far as I remember, the only way I found, was writing the (shell)-variable value to a file, and reading it from awk. If I could only find that old script .... Hope that helps Stefan
arnold@mathcs.emory.edu (Arnold D. Robbins {EUCC}) (06/03/89)
In article <23206@dhw68k.cts.com> jaff@dhw68k.cts.com (Mark Jaffe) writes: >Awk seems to be lacking something, but maybe it's really there. I want to >match a pattern that I don't know until I execute the script, so I want to >pass in a pattern to match in a variable. [...] In nawk (available for big bucks in S5R3.1, or for little bucks from the toolchest), and in GNU Awk (available via anonymous ftp from prep.ai.mit.edu and anonymous uucp from ohio-state for free) regular expression matching can now be dyamnic. The right hand operand of ~ and !~ is treated as a string containing a regular expression. Thus, you can do stuff like $1 ~ $2 { ..... } if you care to. Check out the book by Aho, Kernighan, and Weinberger, or the GNU Awk manual (co-authored by yours truly). Enjoy, -- Arnold Robbins -- Emory University Computing Center | Unix is a Registered DOMAIN: arnold@unix.cc.emory.edu | Bell of AT&T Trademark UUCP: gatech!emoryu1!arnold PHONE: +1 404 727-7636 | Laboratories. BITNET: arnold@emoryu1 FAX: +1 404 727-2599 | -- Donn Seeley
mark@siva.UUCP (Mark Charles Marsh) (06/03/89)
> In article <23206@dhw68k.cts.com> jaff@dhw68k.cts.com (Mark Jaffe) writes: > >Awk seems to be lacking something, but maybe it's really there. I want to > >match a pattern that I don't know until I execute the script, so I want to > >pass in a pattern to match in a variable. I can't get this to work! Here's I am using this and it works. Try: <input stream> | awk -f <awk program> VAR=$THING - or awk -f <awk program> VAR=$THING <input file> and reference your variable in your awk program as: printf("var=[%s]\n",VAR); Good luck. -- "Off by an order of magnitude; my specialty!" |\ /| | / | \ / ...!ames!pacbell!sactoh0!siva!uumgr | \ / | | / | \ / ...!csusac!unify!siva!uumgr | \/ | | \ | \ / Mark Marsh | | | \ | \/
jpr@dasys1.UUCP (Jean-Pierre Radley) (06/07/89)
In article <23206@dhw68k.cts.com>, jaff@dhw68k.cts.com (Mark Jaffe) writes: > Awk seems to be lacking something, but maybe it's really there. I want to > match a pattern that I don't know until I execute the script, so I want to > pass in a pattern to match in a variable. I can't get this to work! Here's > what I'm doing, on a Sun: > SYSVER=`awk '{print $3}' < /etc/motd` > awk ' $0 ~ $SYSVER { print substr($0, 3, length - 2) } ' < cs35if.h > cs35.if If you wanted to really get into hairy quotation mechanisms, you could even extract your /etc/motd info from within awk. But this should work for you: SYSVER=`awk '{print $3}' < /etc/motd` awk " /"$SYSVER"/ { print substr($0, 3, length - 2) } " < cs35if.h > cs35.if BTW, I think it faster to change your first line to: set `/etc/motd` ; SYSVER=$3 -- Jean-Pierre Radley CIS: 72160,1341 jpr@jpradley.UUCP