# AWK運算符

## 算術運算符

AWK支持以下算術運算符：

### 加法

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

(a + b) = 70

### 減法

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

(a - b) = 30

### 乘法

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

(a * b) = 1000

### 除法

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

(a / b) = 2.5

### 模

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

(a % b) = 10

## 遞增和遞減運算符

AWK支持下增量和減量運算符：

### 前遞增

awk 'BEGIN { a = 10; b = ++a; printf "a = %d, b = %d\n", a, b }'

a = 11, b = 11

### 前減量

[jerry]\$ awk 'BEGIN { a = 10; b = --a; printf "a = %d, b = %d\n", a, b }'

a = 9, b = 9

### 後遞增

[jerry]\$ awk 'BEGIN { a = 10; b = a++; printf "a = %d, b = %d\n", a, b }'

a = 11, b = 10

### 後遞減

[jerry]\$ awk 'BEGIN { a = 10; b = a--; printf "a = %d, b = %d\n", a, b }'

a = 9, b = 10

## 賦值運算符

AWK支持以下賦值運算符：

### 簡單的賦值

[jerry]\$ awk 'BEGIN { name = "Jerry"; print "My name is", name }'

My name is Jerry

### 簡寫加法

[jerry]\$ awk 'BEGIN { cnt=10; cnt += 10; print "Counter =", cnt }'

Counter = 20

### 簡寫減法

[jerry]\$ awk 'BEGIN { cnt=100; cnt -= 10; print "Counter =", cnt }'

Counter = 90

### 簡寫乘法

[jerry]\$ awk 'BEGIN { cnt=10; cnt *= 10; print "Counter =", cnt }'

Counter = 100

### 簡寫除法

[jerry]\$ awk 'BEGIN { cnt=100; cnt /= 5; print "Counter =", cnt }'

Counter = 20

### 簡寫模運算

[jerry]\$ awk 'BEGIN { cnt=100; cnt %= 8; print "Counter =", cnt }'

Counter = 4

### 簡寫指數級運算

[jerry]\$ awk 'BEGIN { cnt=2; cnt ^= 4; print "Counter =", cnt }'

Counter = 16

### 簡寫指數級

[jerry]\$ awk 'BEGIN { cnt=2; cnt **= 4; print "Counter =", cnt }'

Counter = 16

## 關係運算符

AWK支持以下關係運算符：

### 等於

awk 'BEGIN { a = 10; b = 10; if (a == b) print "a == b" }'

a == b

### 不等於

[jerry]\$ awk 'BEGIN { a = 10; b = 20; if (a != b) print "a != b" }'

a != b

### 小於

[jerry]\$ awk 'BEGIN { a = 10; b = 20; if (a < b) print "a < b" }'

a < b

### 小於或等於

[jerry]\$ awk 'BEGIN { a = 10; b = 10; if (a <= b) print "a <= b" }'

a <= b

### 大於

[jerry]\$ awk 'BEGIN { a = 10; b = 20; if (b > a ) print "b > a" }'

b > a

### 大於或等於

[jerry]\$ awk 'BEGIN { a = 10; b = 10; if (a >= b) print "a >= b" }'

b >= a

## 邏輯運算符

AWK支持以下邏輯運算符：

### 邏輯與

expr1 && expr2

[jerry]\$ awk 'BEGIN {num = 5; if (num >= 0 && num <= 7) printf "%d is in octal format\n", num }'

5 is in octal format

### 邏輯或

expr1 || expr2

[jerry]\$ awk 'BEGIN {ch = "\n"; if (ch == " " || ch == "\t" || ch == "\n") print "Current character is whitespace." }'

Current character is whitespace.

### 邏輯非

! expr1

[jerry]\$ awk 'BEGIN { name = ""; if (! length(name)) print "name is empty string." }'

name is empty string.

## 三元運算符

condition expression ? statement1 : statement2

[jerry]\$ awk 'BEGIN { a = 10; b = 20; (a > b) ? max = a : max = b; print "Max =", max}'

Max = 20

## 一元運算符

AWK支持以下一元運算符：

### 一元加法運算

[jerry]\$ awk 'BEGIN { a = -10; a = +a; print "a =", a }'

a = -10

### 負號

[jerry]\$ awk 'BEGIN { a = -10; a = -a; print "a =", a }'

a = 10

## 指數型運算符

### 指數型

[jerry]\$ awk 'BEGIN { a = 10; a = a ^ 2; print "a =", a }'

a = 100

### 指數型

[jerry]\$ awk 'BEGIN { a = 10; a = a ** 2; print "a =", a }'

a = 100

## 字符串連接運算符

[jerry]\$ awk 'BEGIN { str1="Hello, "; str2="World"; str3 = str1 str2; print str3 }'

Hello, World

## 數組成員運算符

[jerry]\$ awk 'BEGIN { arr[0] = 1; arr[1] = 2; arr[2] = 3; for (i in arr) printf "arr[%d] = %d\n", i, arr[i] }'

arr[0] = 1
arr[1] = 2
arr[2] = 3

## 正則表達式運算符

### 匹配

[jerry]\$ awk '\$0 ~ 9' marks.txt

1. Rahul Maths 90
2. Hari History 89

### 不匹配

[jerry]\$ awk '\$0 !~ 9' marks.txt

1. Amit Physics 80
2. Shyam Biology 87
3. Kedar English 85