Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!wuarchive!uwm.edu!ux1.cso.uiuc.edu!tank!mimsy!chris From: chris@mimsy.umd.edu (Chris Torek) Newsgroups: comp.unix.questions Subject: Re: Import variables in to awk. Message-ID: <20774@mimsy.umd.edu> Date: 15 Nov 89 14:14:02 GMT References: <10531@thorin.cs.unc.edu> <15919@bloom-beacon.MIT.EDU> Distribution: na Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 53 >In article <10531@thorin.cs.unc.edu> warner@unc.cs.unc.edu (Byron Warner) >writes: [file foo] >>{ print import,$0 } [command] >>awk -F: -f foo /etc/passwd import='hello >>why do I get just a list of logins? In article <15919@bloom-beacon.MIT.EDU> jik@athena.mit.edu (Jonathan I. Kamens) writes: > First of all, I have never known the C-shell to allow the syntax >"foo=bar" on a command-line to import a variable into a program. It does not. However, awk does. That is, you are looking at the wrong program. > Second, the only way to do what you want is to actually make the >creation of this variable part of the awk script. Like this: Not so: Within some limits, you can set awk variables from its invocation. For instance: % cat t BEGIN { print "BEGIN: " this; } { print "INPUT: " this " " $0; } END { print "END: " this; } % cat u first line second line % awk -f t u this=that BEGIN: INPUT: first line INPUT: second line END: that % awk -f t this=that u BEGIN: INPUT: that first line INPUT: that second line END: that % rm t u The `BEGIN' statement is done before any `files' are opened; the `END' statement is done after all `files' have been read. Any `files' of the form `a=b' set variable `a' to value `b'. All of the above is with respect to the 4.3BSD flavour of `awk'. The new awk (as described in the awk book) appears to open the first `file' before executing the BEGIN statement, so that any assignments that appear before the first real file happen before the BEGIN. What GNU awk does, I do not know (but the above technique will tell you). -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@cs.umd.edu Path: uunet!mimsy!chris