Previous Table of Contents Next

Logical Operators

You can use logical operators to combine expressions to form logical expressions. There are three logical operators. They are listed in the following table.

Operator Function

&& Logical AND. For instance,
if ( ( NR > 10 ) && ( NF > 5 ) )

prints each line after line 10 that has five or more fields.

|| Logical OR. For example,
if ( ( NR < 10 ) || ( NF > 7 ) )

prints each line before line 10 or any line with more than seven fields.

! Logical NOT. For instance,
if ! ( NF == 6 )

prints each line that does not have six fields.

The logical operators may be followed by a new-line. This allows for more readable statements when multiple logical operators are used in the same statement.

Increment and Decrement Operators

As in the C programming language, nawk also provides the unary increment and decrement operators. You use these two operators to increment number variables and decrement them.

Operator Function

++ Increment the value by 1. For example,

is the same as i = i + 1.

-- Decrement the value by 1.

Conditional Expressions

The conditional expression is used for simple if, then, else statements or conditions. The general format of a conditional expression is:

     expr1 ? expr2 : expr3

where expr1 is evaluated and if true value of expr2 is used, else the value of expr3 is used. For example,

     print $1 == 1 ? $2 : $3

prints the value of field two ($2) if field one ($1) is equal to 1, else the value of field 3 ($3) is printed.


There are three different categories for functions. The built-in arithmetic functions, built-in string functions, and the user-definable functions. Functions are often referred to as subroutines in some programming languages. They allow you to write one set of statements and then reference them multiple times from different locations in the program. They also provide a means of making your program more understandable and readable.

Arithmetic Functions  The arithmetic functions perform basic mathematical processing.

Function Value Returned

atan2(y,x) Returns the arctangent of y/x in the range -pi to pi.
cos(x) Returns the cosine of x, with x being in radians.
exp(x) Returns the value of ex.
int(x) Returns the integer value of x. Truncates x.
log(x) Returns the natural (base e) logarithm of x.
rand(!) Returns a random number r, 0 <= r < 1.
sin(x) Returns the sine of x, with x being in radians.
sqrt(x) Returns the square root of x.
srand(x) Uses x as the new seed for the rand() function.

String Functions  The string functions provide basic string manipulation functions. The following table describes the functions.

Function Value Returned

delete array [element] Deletes the specified element of the array.
gsub(r,s) Substitutes s in place of r globally in $0 and returns the number of substitutions made.
gsub(r,s,t) Substitutes s in place of r globally in sting t and returns the number of substitutions made.
index(s,t) Returns the position where string t first occurs in string s. Returns 0 if t is not present.
length(s) Returns the number of characters in s.
match(s,r) Searches s for a substring matching r. The index of r is returned or 0. Sets the RSTART and RLENGTH variables.
split(s,a) Splits string s into elements of array a based on fields defined by the field separator specified by FS.
split(s,a,fs) Splits string s into elements of array a based on fields defined by the field separator fs.
sprintf(fmt,expr-list) Returns the expr-list formatted according to the format string fmt.
sub(r,s) Searches $0 for the longest substring matching string r and replaces with string s.
sub(r,s,t) Search string t for the longest substring matching string r and replace with string s.
substr(s,p) Returns the suffix of s starting at position p.
substr(s,p,n) Returns the substring of s of length n starting at position p.

User-Defined Functions  In addition to the built-in functions, nawk provides you with the ability to define your own functions. The format of a function is

     function name( parameter-list ) {

Note that there cannot be any spaces between the name of the function and the left parenthesis. Functions can be defined anywhere a pattern-action statement is found. Functions provide a way to perform the same code multiple times without rewriting the code multiple times.

The return statement can be used to exit a function but is not necessary. The statements within the function are executed and when the last statement completes execution, the function is exited and control is returned to the line after the function call was made.

Previous Table of Contents Next