Previous Table of Contents Next


Functions may call themselves, thus being recursive. When a variable is used as an argument to a function, only a copy of the variable's value is passed to the function, thus the function cannot alter the value of the variable. The elements of an array can, however, be changed. New arrays can be created within functions and returned to the main program. If a variable is not listed in the variable list, then it is a global variable and its value can be manipulated within the function.

Input Control

You can have nawk read input in several ways. The most common is from a file or from the standard input. For example,

     nawk 'NF == 4' fruits

reads input from the file fruits. To have the data read in from the standard input you can either pipe the data to nawk, re-direct it from a file, or enter it from your keyboard. For example,

     dbdownload | nawk -f dbreport1

could be used to take the output of a program and send if through an nawk program to format the output for a particular report.

Input Separators  The input read by nawk is parsed into separate fields. The fields are separated based on the value of the FS variable. The default value of FS is a single space (" "). This informs nawk to consider spaces and tabs as field separators. The leading spaces and tabs are discarded and the first character that is not a space or tab begins the first field. The FS variable may contain regular expressions, allowing for any form of a field separator. To set the FS to a single space you use the regular expression "[ ]".

Input Records  The input can also be parsed into separate records. By default, records and lines are synonymous. Because the default record separator variable RS is set to "\n," the RS variable can be reset to the null string "" to cause the record separator to be a blank line.

The getline Function  The getline function provides a method of reading data from various sources. It can read input from the current input (pipe or data), from a specific file, or from an internal pipe. Depending on how you use getline, various variables are updated. The following table describes each format and how each variable is affected.


NOTE:  
The cmd in the following commands may be a constant or a variable.




Command Description

getline Reads the next line of the current input. The input line is assigned to $0, NF is set to the number of fields, NR and FNR are incremented. For example,
if ( $1 == "C" ) {
     FIRSTLINE=$0
     getline
     SECONDLINE=$0
}

allows you to read a second line by choice instead of looping through the next entire iteration of thenawk program.

getline var Reads the next line of the current input. The input line is assigned to variable var, NR and FNR are incremented.
getline < file Reads the next line of file. The input line is assigned to $0 and NF is set. For example,
while ( getline < $2 )
  print

prints the entire contents of a file whose name was retrieved from the second field of the current input.

getline var < file Reads the next line of file. The input line is assigned to variable var.
cmd | getline Reads the next line of output from command cmd. The input is assigned to $0 and NF is set. When using pipes, getline reads one line of the output produced by the command each time the getline command is executed. An example of using this format
while ( "who" | getline )
   print "USER="$1 "     TTY="$3

reads the output of the UNIX who command and prints out the user name and tty number.

cmd | getline var Reads the next line of output from command cmd. The input is assigned to the variable var.

Output Control

There are two output producing statements. They are the print and printf commands. The print command is for simple output. The printf is for precise formatted output. The output can be redirected to files or pipes. Filenames and pipes may be constants, variables, or expressions.


NOTE:  
The cmd and file in the following commands may be a constant enclosed in double quotes or a variable containing the actual command.



The following table describes the print command.


Command Description

print Prints the entire current line ($0) to the standard output.
print expr, expr ... Prints the expressions expr separated by the output separator, OFS. The default for OFS is a single space. The output from each print command is terminated with the output record separator, ORS. The ORS default is a new-line.
print expr .. > file Prints the expressions to the file named file. The file is overwritten.
print expr .. >> file Prints the expressions to the file named file. The output is appended to the end of the file.
print expr .. | cmd Prints the expressions to the standard input of the command cmd. For example,
for ( E in A )
 print A[E] | "sort > sorted 2>&1"
 close("sort > sorted 2>&1")

prints the contents of each element of the array A and pipes the output to the UNIX sort command. The sort command redirects standard error and standard output to the file sorted. A pipe must be closed using the close command.



Previous Table of Contents Next