This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
由上可知,所用的 awk 版本是 gawk 3.1.4
awk 還有其它命令列選項,欲知詳情,請下:
ols3@mybk:~$ awk --help
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options: GNU long options:
-f progfile --file=progfile
-F fs --field-separator=fs
-v var=val --assign=var=val
-m[fr] val
-W compat --compat
-W copyleft --copyleft
-W copyright --copyright
-W dump-variables[=file] --dump-variables[=file]
-W gen-po --gen-po
-W help --help
-W lint[=fatal] --lint[=fatal]
-W lint-old --lint-old
-W non-decimal-data --non-decimal-data
-W profile[=file] --profile[=file]
-W posix --posix
-W re-interval --re-interval
-W source=program-text --source=program-text
-W traditional --traditional
-W usage --usage
-W version --version
To report bugs, see node `Bugs' in `gawk.info', which is
section `Reporting Problems and Bugs' in the printed version.
gawk is a pattern scanning and processing language.
By default it reads standard input and writes standard output.
BEGIN {
if (1.4142759688)
print "真值"
if ("佳里鎮")
print "真值"
if (x = 66)
print "真值"
}
要特別注意的是: 0 為假值; 但 "0" 卻為真值,因為 "0" 為非空字串。
4.7. awk 的關係式
這一節說明 awk 的比較關係式
若 x 小於 y ,則 x < y 為真。
若 x 小於或等於 y ,則 x <= y 為真。
若 x 大於 y ,則 x > y 為真。
若 x 大於或等於 y ,則 x >= y 為真。
若 x 等於 y ,則 x == y 為真。
若 x 不等於 y ,則 x != y 為真。
若 x 合於樣式 y ,則 x ~ y 為真。 例如: x="I am a foo !"; x ~ /foo/
若 x 不合於樣式 y ,則 x !~ y 為真。 例如: x="I am a spider man"; x !~ /foo/
注意! 經常犯的錯誤是把 if (x == y) 寫成 if ( x = y )
另外,字串也可以比較大小,比如 BEGIN { x="abc"; if ( x < "hij" ) ...},這是依二個字串各對應字母比較其 ASCII 值的大小,來決定整個字串的大小。
4.8. awk 的邏輯關係式
這一節說明 awk 的邏輯關係式
awk 有三種邏輯運算子,而且這三個可任何組合成邏輯關係式。
&& 且(and)
它的意思是: 如果 '判斷式' 為真,則傳回 'A值' 給 '變數C',否則傳回 'B值'。
用例:
#! /usr/bin/awk -f
#
{
# 傳回 $1 和 $2 的較大值
max = ($1 >= $2) ? $1 : $2
if (max > MAX) MAX = max
}
END {
print "最大的是", MAX
}
7.5. while 迴圈
while 迴圈的語法如下:
while (若判斷式為真) {
動作
}
它的意思是說: 如果判斷式為真,則持續做 {} 迴圈中的 '動作',直到判斷式變假才停止。
用例:
#! /usr/bin/awk -f
BEGIN {
while ( getline < "access.log" ) {
total_lines++
}
print "這個檔案共有", total_lines, "列"
}
====
執行結果:
./t1.awk
這個檔案共有 2345 列
無窮迴圈:
#! /usr/bin/awk -f
BEGIN {
while ( 1 ) {
print "I love u."
}
}
7.6. do-while 迴圈
do-while 迴圈的語法如下:
do {
動作
} while (若判斷式為真)
它的意思是說: 第一次,無條件就執行 {} 中的動作,接著會對 while 後面的判斷式,進行判斷,若為真,則持續做 {} 迴圈中的 '動作',直到判斷式變假才停止。
用例:
#! /usr/bin/awk -f
BEGIN {
x=1
do {
sum += x
x++
} while (x<=100)
}
7.7. 幫你的程式碼加上列號
以下這支程式,可以把你的程式碼加上列號,並轉成 HTML 格式。
add-line-no.awk
#! /usr/bin/awk -f
#
# written by OLS3 (ols3@lxer.tw)
#
# 幫程式碼加上列號, 並以 HTML 格式呈現
#
BEGIN {
# 先計算總列數
while ( getline < ARGV[1] ) {
total_lines++
}
T = length(total_lines)
print "<html><head><meta http-equiv=\"Content-type\" content=\"text/html; charset=big5\"></head><body><pre>\n";
}