AWK - Arithmetic Operators


Advertisements

AWK supports the following arithmetic operators −

Addition

It is represented by plus (+) symbol which adds two or more numbers. The following example demonstrates this −

Example

[jerry]$ awk 'BEGIN { a = 50; b = 20; print "(a + b) = ", (a + b) }'

On executing this code, you get the following result −

Output

(a + b) = 70

Subtraction

It is represented by minus (-) symbol which subtracts two or more numbers. The following example demonstrates this −

Example

[jerry]$ awk 'BEGIN { a = 50; b = 20; print "(a - b) = ", (a - b) }'

On executing this code, you get the following result −

Output

(a - b) = 30

Multiplication

It is represented by asterisk (*) symbol which multiplies two or more numbers. The following example demonstrates this −

Example

[jerry]$ awk 'BEGIN { a = 50; b = 20; print "(a * b) = ", (a * b) }'

On executing this code, you get the following result −

Output

(a * b) = 1000

Division

It is represented by slash (/) symbol which divides two or more numbers. The following example illustrates this −

Example

[jerry]$ awk 'BEGIN { a = 50; b = 20; print "(a / b) = ", (a / b) }'

On executing this code, you get the following result −

Output

(a / b) = 2.5

Module

It is represented by percent (%) symbol which finds the module division of two or more numbers. The following example illustrates this −

Example

[jerry]$ awk 'BEGIN { a = 50; b = 20; print "(a % b) = ", (a % b) }'

On executing this code, you get the following result −

Output

(a % b) = 10
awk_operators.htm
Advertisements