Import variables in to awk.

Rich Scott richsc at ism780c.isc.com
Fri Nov 17 03:41:57 AEST 1989


In article <15919 at bloom-beacon.MIT.EDU> jik at athena.mit.edu (Jonathan I. Kamens) writes:
>In article <10531 at thorin.cs.unc.edu> warner at unc.cs.unc.edu (Byron Warner)
>writes:
>>My questions is how do you import csh variables into an awk script.
>>for example if I have a file called foo, which contains:
>>{
>>	print import,$0
>>}
>>
>>and I issue the command 
>>awk -F: -f foo /etc/passwd import='hello
>>why do I get just a list of logins?

	Well, apparently awk wants its 'imported' variables specified on
the command line *before* the datafile(s), but this isn't obvious from the
manual page. Someone here told me that the argument parsing may not be done
correctly. Anyway, on my system, which runs SunOS3.5, I get the desired effect
(using csh) by doing:  awk -F: -f foo import='hello' /etc/passwd

(This is running the 4.2 or 4.3 BSD 'awk'; I can't speak for the "new" awk.)

>
>  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.  C
>shell doesn't have anything like that.

	Umm, I don't think it's up to the shell in this case to do
anything with it; it's simply an argument to the program. Perhaps Byron,
if he really wants to import a C-shell variable into awk, should do:

	hostname% setenv VAR='hello'
	hostname% awk -F: -f foo.awk import=$VAR /etc/passwd

	The first example doesn't set any C-shell variables.
 
----------------
        rich scott                              rls at i88.isc.com
        interactive systems corporation         voice: (800) LAI-UNIX x255
        (formerly lachman associates)           naperville, il, usa



More information about the Comp.unix.questions mailing list