z/OS UNIX System Services User's Guide
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


Arithmetic operators

z/OS UNIX System Services User's Guide
SA23-2279-00

The following awk program uses simple arithmetic:
$3 > 10 { print $1, $2, $3–10 }
In the print statement:
$3–10
has the value of the third field in the record, minus 10. This is the value that print prints. If you apply this program to the hobbies file, the output is:
Jim reading 5
Linda bridge 2
Katie jogging 4
Andrew wind surfing 10
Lori weight lifting 2
You could describe how the program works like this: If someone spends more than 10 hours on a hobby, the program prints the person's name, the name of the hobby, and how many extra hours the person spends on the hobby (that is, the number of hours more than 10).
An expression such as:
$3–10
is called an arithmetic expression. It performs an arithmetic operation and comes up with a result, which is called the value of the expression.

awk recognizes the following arithmetic operations:

Operation Operator Example
Addition A + B 2+3 is 5
Subtraction AB 7–3 is 4
Multiplication A * B 2*4 is 8
Division A / B 6/3 is 2
Negation A – 9 is –9
Remainder A % B 7%3 is 1
Exponentiation A ^ B 3^2 is 9
The remainder operation is also known as the modulus, or integer remainder operation. The value of this expression is the integer remainder you get when you divide A by B. For example:
7 % 3
has a value of 1, because dividing 7 by 3 gives you 2 with a remainder of 1.
The value for the exponentiation operation:
A ^ B
is the value of A raised to the exponent B. For example:
3 ^ 2
has the value 9 (that is, 32).

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014