初识gawk
shell 腳本最常見的用途之一是處理文本文件.
檢查日志文件, 讀取配置文件, shell 腳本可以幫助我們將一下日常處理任務(wù)自動(dòng)化. sed 和 gawk 工具可以極大簡化文本數(shù)據(jù)處理任務(wù).
sed 是 流編輯器(stream editor).
普通的交互式文本編輯器(如vim): 用鍵盤命令交互地插入, 刪除, 替換文本;
流編輯器: 在編輯器處理數(shù)據(jù)基于 預(yù)先提供的一組規(guī)則.
sed 雖然功能強(qiáng)大, 但是今天我們不介紹它.
因?yàn)槲覀円榻B一個(gè)更強(qiáng)大的 gawk.
1 gawk 簡介
gawk 是 Unix 中原始的 awk 程序的 GNU 版本.
我的 Linux 系統(tǒng)里的 awk 命令是通過 軟鏈接 指向 gawk 的:
gawk 程序讓 流編輯 邁上了一個(gè)新的臺階, 因?yàn)?gawk 提供了一種編程語言而不是 流編輯命令.
gawk 命令的基本格式:
gawk options program filegawk 會針對 數(shù)據(jù)流 的 每行文本 執(zhí)行程序腳本(program).
在命令行中, program 必須包在 '' 中.
而行 數(shù)據(jù)處理 的 program 需要放在花括號 {} 中, 所以 命令行中數(shù)據(jù)處理程序 可以寫成 '{program}'.
數(shù)據(jù)處理 之前的 program 放在 BEGIN {} 的花括號中, 數(shù)據(jù)處理 之后的 program 放在 END {} 的花括號中.
gawk 的 可選項(xiàng)(options) :
| -F fs | field-separator 指定行中劃分?jǐn)?shù)據(jù)字段的 字段分隔符 |
| -f file | 從指定的文件中讀取程序 |
| -v var=value | 定義 gawk 程序中的一個(gè)變量及其默認(rèn)值 |
| -mf N | 指定文件中的最大字段數(shù) |
| -mr N | 指定文件中最大數(shù)據(jù)行數(shù) |
| -W keyword | 指定gawk 的兼容模式 或 警告等級 |
gawk 的強(qiáng)大之處在于 程序腳本(program), 可以寫腳本來處理文本行數(shù)據(jù).
gawk 腳本程序 用 一對花括號 {} 來定義. 必須把 腳本命令 放到 花括號{} 中.
看個(gè)示例:
$ gawk '{print "hello world"}' 123 hello world 456 hello world ==== hello world ^C上面的例子中, 不論在命令行輸入啥, gawk 總是打印一句 “hello world”.
print 命令用于將文本打印到 STDOUT. 由于例子中沒有在 命令行 指定文件名, 所以 gawk 會從 STDIN 接收數(shù)據(jù). 本例中, gawk 會一直等待從 STDIN 輸入.
2 使用數(shù)據(jù)字段變量
$0 代表整個(gè)文本行.
$1 代表第 1 個(gè)數(shù)據(jù)字段.
$2 代表第 2 個(gè)數(shù)據(jù)字段.
$n 代表第 n 個(gè)數(shù)據(jù)字段.
gawk 讀取文件中的第一個(gè)字段 $1:
$ cat test.txt one line of test text. two line of test text. three line of test text.$ gawk '{print $1}' test.txt one two three使用 -F 選項(xiàng)指定 字段分隔符 :
$ gawk -F: '{print $1}' /etc/passwd root bin daemon adm lp ...如果 program 有多條命令, 命令之間可以用 分號(;) 分隔, 也可以直接換行:
$ echo "my name is miyan" | gawk '{$4="rosie"; print $0}' my name is rosie$ echo "my name is miyan" | gawk '{$4="rosie" pipe quote> print $0}' my name is rosie上面的 pipe quote> 是命令行中的 次提示符, 提示命令輸入更多的數(shù)據(jù), 直到輸入了結(jié)尾的 單引號(') 才會結(jié)束.
3 從文件中讀取程序program
從 文件中讀取 gawk 的 program, 只需要使用 -f file_name 即可:
$ cat t.gawk {print $1 "'s home directory is " $6 }$ gawk -F: -f t.gawk /etc/passwd root's home directory is /root bin's home directory is /bin daemon's home directory is /sbin adm's home directory is /var/adm ...gawk program 文件中使用變量值 不需要 像 shell 腳本一樣加 $:
$ cat t.gawk {text = "'s home directory is "print $1 text $6 }$ gawk -F: -f t.gawk /etc/passwd root's home directory is /root bin's home directory is /bin daemon's home directory is /sbin adm's home directory is /var/adm ...4 在處理數(shù)據(jù)前/后運(yùn)行腳本
BEGIN 關(guān)鍵字可以強(qiáng)制 gawk 在讀取數(shù)據(jù)前, 先執(zhí)行 BEGIN 關(guān)鍵字后面的 program:
$ gawk 'BEGIN {print "hello world"}' hello world$ cat test.txt one line of test text. two line of test text. three line of test text.$ gawk 'BEGIN {print "file contents: "} {print $0}' test.txt file contents: one line of test text. two line of test text. three line of test text.與 BEGIN 類似, END 關(guān)鍵字會使 gawk 在讀取完數(shù)據(jù)以后執(zhí)行 END 關(guān)鍵字后的 program:
$ gawk 'BEGIN {print "file contents: "} quote> {print $0} quote> END {print "end of file"}' test.txt file contents: one line of test text. two line of test text. three line of test text. end of fileReference
[1]. Linux命令行與shell腳本編程大全(第三版)
總結(jié)
- 上一篇: spring源码构建时缺失spring-
- 下一篇: 计算机网络自动化,自动化博览