在 awk 系列的第八節,我們介紹了一些強大的 awk 指令功能,它們是變量、數字表達式和指派運算符。
本節我們将學習更多的 awk 功能,即 awk 的特殊模式:<code>begin</code> 和 <code>end</code>。
學習 awk 的模式 begin 和 end
随着我們逐漸展開,并探索出更多建構複雜 awk 操作的方法,将會證明 awk 的這些特殊功能的是多麼強大。
開始前,先讓我們回顧一下 awk 系列的介紹,記得當我們開始這個系列時,我就指出 awk 指令的通用文法是這樣的:
<code># awk 'script' filenames</code>
在上述文法中,awk 腳本擁有這樣的形式:
<code>/pattern/ { actions }</code>
你通常會發現腳本中的模式(<code>/pattern/</code>)是一個正規表達式,此外,你也可以在這裡用特殊模式 <code>begin</code> 和<code>end</code>。是以,我們也能按照下面的形式編寫一條 awk 指令:
<code>awk '</code>
<code>begin { actions }</code>
<code>……….</code>
<code>end { actions }</code>
<code>' filenames</code>
假如你在 awk 腳本中使用了特殊模式:<code>begin</code> 和 <code>end</code>,以下則是它們對應的含義:
<code>begin</code> 模式:是指 awk 将在讀取任何輸入行之前立即執行 <code>begin</code> 中指定的動作。
<code>end</code> 模式:是指 awk 将在它正式退出前執行 <code>end</code> 中指定的動作。
含有這些特殊模式的 awk 指令腳本的執行流程如下:
當在腳本中使用了 <code>begin</code> 模式,則 <code>begin</code> 中所有的動作都會在讀取任何輸入行之前執行。
然後,讀入一個輸入行并解析成不同的段。
接下來,每一條指定的非特殊模式都會和輸入行進行比較比對,當比對成功後,就會執行模式對應的動作。對所有你指定的模式重複此執行該步驟。
再接下來,對于所有輸入行重複執行步驟 2 和 步驟 3。
當讀取并處理完所有輸入行後,假如你指定了 <code>end</code> 模式,那麼将會執行相應的動作。
當你使用特殊模式時,想要在 awk 操作中獲得最好的結果,你應當記住上面的執行順序。
為了便于了解,讓我們使用第八節的例子進行示範,那個例子是關于 tecmint 擁有的域名清單,并儲存在一個叫做 domains.txt 的檔案中。
<code>news.tecmint.com</code>
<code>tecmint.com</code>
<code>linuxsay.com</code>
<code>windows.tecmint.com</code>
<code>$ cat ~/domains.txt</code>
檢視檔案内容
在這個例子中,我們希望統計出 domains.txt 檔案中域名 <code>tecmint.com</code> 出現的次數。是以,我們編寫了一個簡單的 shell 腳本幫助我們完成任務,它使用了變量、數學表達式和指派運算符的思想,腳本内容如下:
<code>#!/bin/bash</code>
<code>for file in $@; do</code>
<code>if [ -f $file ] ; then</code>
<code>### 輸出檔案名</code>
<code>echo "file is: $file"</code>
<code>### 輸出一個遞增的數字記錄包含 tecmint.com 的行數</code>
<code>awk '/^tecmint.com/ { counter+=1 ; printf "%s\n", counter ; }' $file</code>
<code>else</code>
<code>### 若輸入不是檔案,則輸出錯誤資訊</code>
<code>echo "$file 不是一個檔案,請指定一個檔案。" >&2 && exit 1</code>
<code>fi</code>
<code>done</code>
<code>### 成功執行後使用退出代碼 0 終止腳本</code>
<code>exit 0</code>
現在讓我們像下面這樣在上述腳本的 awk 指令中應用這兩個特殊模式:<code>begin</code> 和 <code>end</code>:
我們應當把腳本:
改成:
<code>awk ' begin { print "檔案中出現 tecmint.com 的次數是:" ; }</code>
<code>/^tecmint.com/ { counter+=1 ; }</code>
<code>end { printf "%s\n", counter ; }</code>
<code>' $file</code>
在修改了 awk 指令之後,現在完整的 shell 腳本就像下面這樣:
<code>### 輸出檔案中 tecmint.com 出現的總次數</code>
awk 模式 begin 和 end
當我們運作上面的腳本時,它會首先輸出 domains.txt 檔案的位置,然後執行 awk 指令腳本,該指令腳本中的特殊模式 <code>begin</code> 将會在從檔案讀取任何行之前幫助我們輸出這樣的消息“<code>檔案中出現 tecmint.com 的次數是:</code>”。
接下來,我們的模式 <code>/^tecmint.com/</code> 會在每個輸入行中進行比較,對應的動作 <code>{ counter+=1 ; }</code> 會在每個比對成功的行上執行,它會統計出 <code>tecmint.com</code> 在檔案中出現的次數。
最終,<code>end</code> 模式将會輸出域名 <code>tecmint.com</code> 在檔案中出現的總次數。
<code>$ ./script.sh ~/domains.txt</code>
用于統計字元串出現次數的腳本
原文釋出時間為:2016-08-06
本文來自雲栖社群合作夥伴“linux中國”