formats numbers using OFMT or "%d" for exact integers. "%c" with a numeric argument prints the corresponding 8 bit character,
with a string argument it prints the first character of the string. The output of print and printf can be redirected to a file or command by appending > file, >> file or | command to the end of the print statement.
Redirection opens file or command only once, subsequent redirections append to the already open stream. By convention, mawk associates the filename
• "/dev/stderr" with stderr,
• "/dev/stdout" with stdout,
• "-" and "/dev/stdin" with stdin.
The association with stderr is especially useful because it allows print and printf to be redirected to stderr. These names can also be passed to functions.
The input function getline has the following variations.
getline
reads into $0, updates the fields, NF, NR and FNR.
getline < file
reads into $0 from file, updates the fields and NF.
getline var
reads the next record into var, updates NR and FNR.
getline var < file
reads the next record of file into var.
command | getline
pipes a record from command into $0 and updates the fields and NF.
command | getline var
pipes a record from command into var.
Getline returns 0 on end‐of‐file, -1 on error, otherwise 1.
Commands on the end of pipes are executed by /bin/sh.
The function close(expr) closes the file or pipe associated with expr. Close returns 0 if expr is an open file, the exit status if expr is a piped command, and -1 otherwise. Close is used to reread a file or com‐
mand, make sure the other end of an output pipe is finished or conserve file resources.
The function fflush(expr) flushes the output file or pipe associated with expr. Fflush returns 0 if expr is an open output stream else -1. Fflush without an argument flushes stdout. Fflush with an empty argument
("") flushes all open output.
The function system(expr) uses the C runtime system call to execute expr and returns the corresponding wait status of the command as follows:
• if the system call failed, setting the status to ‐1, mawk returns that value.
• if the command exited normally, mawk returns its exit‐status.
• if the command exited due to a signal such as SIGHUP, mawk returns the signal number plus 256.
Changes made to the ENVIRON array are not passed to commands executed with system or pipes.
10. User defined functions
The syntax for a user defined function is
function name( args ) { statements }
The function body can contain a return statement
return opt_expr
A return statement is not required. Function calls may be nested or recursive. Functions are passed expressions by value and arrays by reference. Extra arguments serve as local variables and are initialized to
null. For example, csplit(s,A) puts each character of s into array A and returns the length of s.
function csplit(s, A, n, i)
{
n = length(s)
for( i = 1 ; i <= n ; i++ ) A[i] = substr(s, i, 1)
return n
}
Putting extra space between passed arguments and local variables is conventional. Functions can be referenced before they are defined, but the function name and the ’(’ of the arguments must touch to avoid confusion
with concatenation.
A function parameter is normally a scalar value (number or string). If there is a forward reference to a function using an array as a parameter, the function’s corresponding parameter will be treated as an array.
11. Splitting strings, records and files
Awk programs use the same algorithm to split