初识gawk
shell 腳本最常見的用途之一是處理文本文件.
檢查日志文件, 讀取配置文件, shell 腳本可以幫助我們將一下日常處理任務自動化. sed 和 gawk 工具可以極大簡化文本數據處理任務.
sed 是 流編輯器(stream editor).
普通的交互式文本編輯器(如vim): 用鍵盤命令交互地插入, 刪除, 替換文本;
流編輯器: 在編輯器處理數據基于 預先提供的一組規則.
sed 雖然功能強大, 但是今天我們不介紹它.
因為我們要介紹一個更強大的 gawk.
1 gawk 簡介
gawk 是 Unix 中原始的 awk 程序的 GNU 版本.
我的 Linux 系統里的 awk 命令是通過 軟鏈接 指向 gawk 的:
gawk 程序讓 流編輯 邁上了一個新的臺階, 因為 gawk 提供了一種編程語言而不是 流編輯命令.
gawk 命令的基本格式:
gawk options program filegawk 會針對 數據流 的 每行文本 執行程序腳本(program).
在命令行中, program 必須包在 '' 中.
而行 數據處理 的 program 需要放在花括號 {} 中, 所以 命令行中數據處理程序 可以寫成 '{program}'.
數據處理 之前的 program 放在 BEGIN {} 的花括號中, 數據處理 之后的 program 放在 END {} 的花括號中.
gawk 的 可選項(options) :
| -F fs | field-separator 指定行中劃分數據字段的 字段分隔符 |
| -f file | 從指定的文件中讀取程序 |
| -v var=value | 定義 gawk 程序中的一個變量及其默認值 |
| -mf N | 指定文件中的最大字段數 |
| -mr N | 指定文件中最大數據行數 |
| -W keyword | 指定gawk 的兼容模式 或 警告等級 |
gawk 的強大之處在于 程序腳本(program), 可以寫腳本來處理文本行數據.
gawk 腳本程序 用 一對花括號 {} 來定義. 必須把 腳本命令 放到 花括號{} 中.
看個示例:
$ gawk '{print "hello world"}' 123 hello world 456 hello world ==== hello world ^C上面的例子中, 不論在命令行輸入啥, gawk 總是打印一句 “hello world”.
print 命令用于將文本打印到 STDOUT. 由于例子中沒有在 命令行 指定文件名, 所以 gawk 會從 STDIN 接收數據. 本例中, gawk 會一直等待從 STDIN 輸入.
2 使用數據字段變量
$0 代表整個文本行.
$1 代表第 1 個數據字段.
$2 代表第 2 個數據字段.
$n 代表第 n 個數據字段.
gawk 讀取文件中的第一個字段 $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 選項指定 字段分隔符 :
$ 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> 是命令行中的 次提示符, 提示命令輸入更多的數據, 直到輸入了結尾的 單引號(') 才會結束.
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 在處理數據前/后運行腳本
BEGIN 關鍵字可以強制 gawk 在讀取數據前, 先執行 BEGIN 關鍵字后面的 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 關鍵字會使 gawk 在讀取完數據以后執行 END 關鍵字后的 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腳本編程大全(第三版)
總結
- 上一篇: spring源码构建时缺失spring-
- 下一篇: 计算机网络自动化,自动化博览