### AWK

```1
AWK 入門
Speaker: Yu-Ping Chen
Date: July 11, 2012
2
Introduction
• Awk是一種可以處理資料、產生格式化報表的語

• Awk的工作方式是讀取資料檔，將每一列資料視

• 需要具備正規表示式的基本知識。
3
Awk 用法(1)
• awk '條件類型1{動作1} 條件類型2{動作2} ...'
filename
• \$ awk '{ print }' /etc/passwd
• \$ awk -F":" '{ print \$1 }' /etc/passwd
• \$ ls -l /etc | awk '{print \$1 "\t" \$9}'
4
Awk 用法(2)
• \$ awk '{print \$2,\$3}' emp_names
• \$ awk '/AL/ {print \$3,\$2}' emp_names
• \$ awk '/AL/ {print \$3,\$2 ; print \$4,\$5}'
emp_names
▫ \$ awk '/AL/ {print \$3,\$2} {print \$4,\$5}' emp_names
▫ \$ awk '/AL/ {print \$3,\$2 ; print \$4", "\$5"\n"}' emp_names
• \$ awk '/AL|IN/' emp_names
5
Awk 的變數(1)
• 變數
▫
▫
▫
▫
▫
▫
NF 每一行 (\$0) 擁有的欄位總數
NR 目前 awk 所處理的是『第幾行』資料
FS 目前的分隔字元，預設是空白鍵
RS 記錄的分隔字元，預設是行
OFS 輸出分隔字元
ORS 輸出記錄分隔字元，預設是行
• \$ last | awk '{print \$1 "\t lines: " NR "\t columes:
" NF}'
6
Awk的變數(2)
• \$ awk '{FS=":"}{print \$2}' mou_names
• \$ awk -F":" '{OFS="-"}{print \$1,\$2,\$3,\$4,\$5}'
mou_names
• \$ awk -F":" '{print NR,\$1,\$2,\$3}' mou_names
• \$ awk -F":" '/4601[2-5]/' mou_names
7
Awk 的運算(1)
• 邏輯運算
▫
▫
▫
▫
▫
▫
> 大於
< 小於
>= 大於或等於
<= 小於或等於
== 等於
!= 不等於
• 數學運算
▫
▫
▫
▫
- subtracts
* multiplies
/ divides
8
Awk 的運算(2)
▫
▫
▫
▫
▫
▫
▫
▫
▫
^ performs exponential mathematics
% gives the modulo
++ adds one to the value of a variable
+= assigns the result of an addition operation to a variable
-- subtracts one from a variable
-= assigns the result of a subtraction operation to a variable
*= assigns the result of multiplication
/= assigns the result of division
%= assigns the result of a modulo operation
9
Awk的運算(3)
\$ awk '{print \$1,"QTY: "\$2,"PRICE: "\$3,"TOTAL:
"\$2*\$3}' inventory
\$ awk '{x=x+\$2} {print x}' inventory
10
Awk的運算(4)
• \$ awk '{x=x+(\$2*\$3)}{print \$1,"QTY:
"\$2,"PRICE: "\$3,"TOTAL: "\$2*\$3,"BAL: "x}'
inventory
• \$ awk '{x=x+(\$2*\$3)} {print \$1,"QTY:
"\$2,"PRICE: "\$3,"TOTAL: "\$2*\$3} END {print
"Total Value of Inventory: " x}' inventory
11
Awk的運算(5)
• \$ sort emp_names | awk '{print \$3,\$2}'
• \$ awk '{print NR, \$1 > "/tmp/filez" }'
emp_names
• \$ awk '{ print \$2 | "sort" }' emp_names
12
Example(1)
\$ cat g-than
#!/bin/awk -f
BEGIN{
lowerbound = 55
}
/^#/ {
print \$0
}
! /^#/ {
for(i=1; i<=NF; i++)
if( \$i > lowerbound )
print \$i
}
\$ cat > N
# just a test
33 22 999 223
44 55 99
\$ ./g-than N
# just a test
999
223
99
13
Example(2)
\$ cat > sum-vars
#!/bin/awk -f
/^#/ {
print \$0
}
! /^#/ {
name = \$1
sum = 0
for(i=2; i<=NF; i++)
sum += \$i
var[ name ] += sum
}
END{
for (v in var) printf "%s = %d\n", v, var[v]
}
\$ cat > testnum
klim 233 112 33
# oh!, just a test
# again
milk 333 22
klim 999 22
oak 222
redcow 1023
oak 11
milk 333 22
\$ ./sum-vars testnum
# oh!, just a test
# again
redcow = 1023
klim = 1399
oak = 233
milk = 710
14
Exercise
• 如何利用 awk 取出帳號與登入者的 IP，且帳號與
IP 之間以 [tab] 隔開？
• /etc/passwd 以冒號 ":" 作為欄位的分隔，如何查

• 利用awk計算出檔案sipmemo有幾行空白行？
15
Reference
• http://www.cyut.edu.tw/~dywang/linuxProgra
m/node20.html
• http://erdos.csie.ncnu.edu.tw/~klim/unixintro/unix-intro-991.html
• http://www.vectorsite.net/tsawk_2.html
```