Import variables in to awk.

Jonathan I. Kamens jik at athena.mit.edu
Wed Nov 15 21:35:39 AEST 1989


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?
>Thanx in Advance

  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.

  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:

% set import = 'hello'
% awk 'BEGIN { import = "'"$import"'" } { print import, $0}' /etc/passwd

The $import is evaluated before awk is actually called, and replaced
by 'hello' (sans quotes).

Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik at Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8495			      Home: 617-782-0710



More information about the Comp.unix.questions mailing list