Import variables in to awk.
Chris Torek
chris at mimsy.umd.edu
Thu Nov 16 01:14:02 AEST 1989
>In article <10531 at thorin.cs.unc.edu> warner at 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 at bloom-beacon.MIT.EDU> jik at 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 at cs.umd.edu Path: uunet!mimsy!chris
More information about the Comp.unix.questions
mailing list