Awk command line parameters
Here is an example.
$ Cat datafile
Northwest NW Joel Craig 3.0. 98 3 4
Western WE Sharon Kelly 5.3. 97 5 23
Southwest SW Chris Foster 2.7. 8 2 18
Southern SO May Chin 5.1. 95 4 15
Southeast SE Derek Johnson 4.0. 7 4 17
Eastern EA Suan Beal 4.4. 84 5 20
Northeast ne tj Nicholas 5.1. 94 3 13
North NO Val Shultz 4.5. 89 5 9
Central CT Sheri Watson 5.7. 94 5 13
$ Cat argvs. SC
# Testing command-line arguments with ARGV and ARGC using a for 1oop.
BEGIN {
For (I = 0; I printf ("argv [% d] is % s \ n", I, ARGV [I]}
Printf ("The number of arguments, ARGC = % d \ n", ARGC)
}
}
$ Awk-f argvs. SC datafile
Argv [O] is nawk
Argv [l] is datafile
The number of arguments, ARGC = 2Description: The BEGIN block contains a for loop for processing command line parameters. ARGC is the number of parameters, and ARGV is an array containing actual parameters. Nawk does not regard options as parameters. In this example, only the nawk command and the input file are valid parameters.
Example
$ Awk 'in in {name = ARGV [1]}; \
$0 ~ Name {print $3, $4} '"Derek" datafile
Awk: can t open Derek
Source line number 1
$ Awk 'in in {name = ARGV [1]; delete ARGV [1]}; \
$0 ~ Name {print $3, $4} '"Derek" datafile
Derek JohnsonDescription
1. In the BEGIN block, the name "Derek" is assigned to the variable name. In the next mode block, awk tries to open "Derek" as the input file, and the result fails.
2. After "Derek" is assigned to the variable name, awk will delete ARGV [1. When you enter the mode block, awk does not try to open "Derek" as the input file, but opens the file datafile.