-->
Page 167
*/% | Multiplication, division, and modulus. |
+_! | Unary plus, unary minus, and logical negation. |
^ | Exponentiation (** may also be used, and **= for the assignment operator). |
++ __ | Increment and decrement, both prefix and postfix. |
$ | Field reference. |
CONTROL STATEMENTS
The control statements are as follows:
if (condition) statement [ else statement ] while (condition) statement do statement while (condition) for (expr1; expr2; expr3) statement for (var in array) statementbreak continue delete array[index] delete array exit [ expression ] { statements }
I/O STATEMENTS
The input/output statements are as follows:
close(filename) | Close file (or pipe, see paragraph following this list). |
getline | Set $0 from next input record; set NF, NR, FNR. |
getline <file | Set $0 from next record of file; set NF. |
getline var | Set var from next input record; set NF, FNR. |
getline var <file | Set var from next record of file. |
next | Stop processing the current input record. The next input record is read and processing starts over with the first pattern in the awk program. If the end of the input data is reached, the END block(s), if any, are executed. |
next file | Stop processing the current input file. The next input record read comes from the next input file. FILENAME is updated, FNR is reset to 1, and processing starts over with the first pattern in the awk program. If the end of the input data is reached, the END block(s), if any, are executed. |
Prints the current record. | |
print expr-list | Prints expressions. Each expression is separated by the value of the OFS variable. The output record is terminated with the value of the ORS variable. |
print expr-list >file | Prints expressions on file. Each expression is separated by the value of the OFS variable. The output record is terminated with the value of the ORS variable. |
printf fmt, expr-list | Format and print. |
printf fmt, expr-list >file | Format and print on file. |
system(cmd-line) | Execute the command cmd-line, and return the exit status. (This may not be available on -POSIX systems.) |
Other input/output redirections are also allowed. For print and printf, >>file appends output to the file, while | command writes on a pipe. In a similar fashion, command | getline pipes into getline. The getline command will return 0 on end of file, and _1 on an error.
THE PRINTF STATEMENT
The awk versions of the printf statement and sprintf() function accept the following conversion specification formats:
An ASCII character. If the argument used for %c is numeric, it is treated as a character and printed. Otherwise, the argument is assumed to be a string, and the only first character of that string is printed. |
Page 168
%d | A decimal number (the integer part). |
%i | Just like %d. |
%e | A floating-point number of the form [_]d.ddddddE[+_]dd. |
%f | A floating-point number of the form [_]ddd.dddddd. |
%g | Use e or f conversion, whichever is shorter, with nonsignificant zeros suppressed. |
%o | An unsigned octal number (again, an integer). |
%s | A character string. |
%x | An unsigned hexadecimal number (an integer). |
%X | Like %x, but using ABCDEF instead of abcdef. |
%% | A single % character; no argument is converted. |
There are optional, additional parameters that may lie between the % and the control letter:
_ | The expression should be left-justified within its field. |
width | The field should be padded to this width. If the number has a leading zero, then the field will be padded with zeros. Otherwise, it is padded with blanks. This applies even to the nonnumeric output formats. |
.prec | A number indicating the maximum width of strings or digits to the right of the decimal point. |
The dynamic width and prec capabilities of the C printf() routines are supported. A * in place of either the width or prec specifications will cause their values to be taken from the argument list to printf or sprintf().
SPECIAL FILEMANES
When doing I/O redirection from either print or printf into a file, or via getline from a file, gawk recognizes certain special filenames internally. These filenames allow access to open file descriptors inherited from gawk's parent process (usually the shell). Other special filenames provide access information about the running gawk process. The filenames are
/dev/pid | Reading this file returns the process ID of the current process, in decimal, terminated with a newline. |
/dev/ppid | Reading this file returns the parent process ID of the current process, in decimal, terminated with a newline. |
/dev/pgrpid | Reading this file returns the process group ID of the current process, in decimal, terminated with a newline. |
/dev/user | Reading this file returns a single record terminated with a newline. The fields are separated with blanks. $1 is the value of the getuid(2) system call, $2 is the value of the geteuid(2) system call, $3 is the value of the getgid(2) system call, and $4 is the value of the getegid( 2) system call. If there are any additional fields, they are the group IDs returned by getgroups(2). Multiple groups may not be supported on all systems. |
/dev/stdin | The standard input. |
/dev/stdout | The standard output. |
/dev/stderr | The standard error output. |
/dev/fd/n | The file associated with the open file descriptor n. |
These are particularly useful for error messages. For example, you could use
print "You blew it!" > "/dev/stderr"
whereas you would otherwise have to use
print "You blew it!" j "cat 1>&2"
These filenames may also be used on the command line to name data files.
NUMERIC FUNCTIONS
awk has the following predefined arithmetic functions:
atan2(y, x) | Returns the arctangent of y/x in radians. |
cos(expr) | Returns the cosine in radians. |