Previous Table of Contents Next

The word "FRUIT" is a string constant that is compared lexigraphically to field 1.

A numeric constant is an unquoted number. The number may be an integer, decimal, or floating point. All numbers are stored as floating points with the precision being machine dependent. Examples of number constants are:



Variables are used to store dynamic values for later use. They can be used in patterns or actions. The type of a variable is not defined initially. Each time a variable is used, nawk decides what type it needs to use. It can be a number or a character type. There are three types of variables used in nawk: built-in, field variables, and user defined.

Built-in Variables  These variables are set and updated by nawk. You can reset the values if you desire, but there is usually no need. All built-in variables have all uppercase names. The following table describes each of the variables.

Variable Description

ARGC The number of command line arguments
ARGV The array containing the command line arguments
ENVIRON Array of environment variables passed in from the shell. Refer to the env or printenv modules.
FILENAME Current filename; contains the name of the current input file being read.
FNR The number of the current record in the current file
NR The number of current records read so far
NF The number of fields on the current record
FS Field separator; defines the characters that separate the current input line into fields. The default is spaces and tabs.
OFMT The format used by printf for numbers. The default is %.6g.
OFS Output field separator; defines the characters that separate the current output line into fields.
ORS Output record separator; defines the characters that separate the output into separate records. The default is new-line.
RLENGTH Length of the string matched by the match function
RS Record separator; defines the characters that separate the input into separate records. The default is new-line.
RSTART The starting position of the string matched by the match function
SUBSEP The subscript separator. The default is \034 (^\).

Field Variables  The fields of the current input record are assigned to the variables $n where n is the field number; field one is assigned to $1. The dollar sign informs nawk to interpret the variable as a field position. The field number may be a constant or a variable. You may reassign values to field positions and the record variable. The record variable is $0; it refers to the entire current input record.

Field Variable Description

$0 Contains the entire current record (input line).
$n Contains value of field n (column n). Where n is a numeric constant. For example,
print $1 " " $2

prints the first and second field separated by a space.

$i Contains value of field i. Where i is a variable or arithmetic expression. For example,
for ( i = 1; i <= NF ; i++ )
    print $i

prints each field on a separate line. You can also reference fields with arithmetic expressions; for instance,

print $(NF - 1)

prints the next to last field of the record.

User definable variables  User definable variables are variables you assign values to as you desire. The variable names may contain letters, digits, and underscores. They must begin with a letter or underscore. You do not have to declare variables in nawk. A variable can be of string type or numeric type. If you reference a variable that has not been assigned a value, nawk uses the null string " " for a string value and zero (0) for a numeric value. You can use a string variable in a numeric expression and you can use a numeric variable in a string expression. Nawk converts the types as required automatically.

VAR A variable named VAR
i=10 Assign the value of 10 to the variable i.
FRUIT="Apple" Assign the string value of "Apple" to FRUIT.

Command Line Variable Assignment

Nawk allows you to assign values to variables on the command line, thus permitting you to pass shell variables and values to the nawk script. These variables appear on the command line in the position of filenames. The syntax must be var=value. These assignment statements can be placed between filenames. This causes nawk to set the variable internally after one file has been read and before the next file is read. For example,

     nawk -f progfile =5,20 infile1 range=55,75 infile2

sets the variable range to 5,20 during the processing of file infile1, then resets the value of range to 55,75 for file infile2.

Not all versions of nawk support the variable and filename interweaving.

Command Line Arguments

The command line arguments are available inside the nawk script. The arguments are placed in a built-in array named ARGV. The number of arguments passed to the script are stored in the built-in variable ARGC. The ARGC variable is set to one more than the actual number of arguments, because the first arguments is always nawk and thus you must add one to the argument count. For example, in the following command ARGC equals 5.

     nawk -f progfile aflag=1 bflag=0 cflag=1 infile

ARGV[0] contains nawk, ARGV[1] contains aflag=1, ARGV[2] contains bflag=0, ARGV[3] contains cflag=1, and ARGV[4] contains infile. This numbering and referencing of arguments scheme is compatible with the C programming language.

The -f progfile, 'program,', and -Fsep are not considered arguments and, therefore, are not assigned to an element of the ARGV array.

Previous Table of Contents Next