AWK簡介及使用例項

還不算暈發表於2013-12-04

AWK簡介及使用方法

awk是一個強大的文字分析工具,相對於grep的查詢,sed的編輯,awk在其對資料分析並生成報告時,顯得尤為強大。簡單來說awk就是把檔案逐行的讀入,以空格為預設分隔符將每行切片,切開的部分再進行各種分析處理。
awk有3個不同版本: awk、nawk和gawk,未作特別說明,一般指gawk,gawk 是 AWK 的 GNU 版本。
awk其名稱得自於它的創始人 Alfred Aho 、Peter Weinberger 和 Brian Kernighan 姓氏的首個字母。實際上 AWK 的確擁有自己的語言: AWK 程式設計語言 , 三位建立者已將它正式定義為“樣式掃描和處理語言”。
它允許您建立簡短的程式,這些程式讀取輸入檔案、為資料排序、處理資料、對輸入執行計算以及生成報表,還有無數其他的功能。
呼叫AWK的三種方法:
1.命令列方式
awk [-F  field-separator]  'commands'  input-file(s)
commands 是真正awk命令,[-F域分隔符]是可選的,預設空格。 input-file(s) 是待處理的檔案
2.shell指令碼方式
將所有的awk命令插入一個檔案,並使awk程式可執行,然後awk命令直譯器作為指令碼的首行,通過鍵入指令碼名稱來呼叫。
相當於shell指令碼首行的:#!/bin/sh換成:#!/bin/awk
3.將所有的awk命令插入一個單獨檔案,然後呼叫:
awk -f awk-script-file input-file(s)  --f選項載入awk-script-file中的awk指令碼,input-file(s)跟上面的是一樣的。
##################################################################################################

awk內建變數

內建變數用來設定環境資訊,這些變數可以被改變,下面給出了最常用的一些變數。
$0變數是指整條記錄。$1表示當前行的第一個域,$2表示當前行的第二個域,......以此類推。
ARGC               命令列引數個數
ARGV               命令列引數排列
ENVIRON            支援佇列中系統環境變數的使用
FILENAME           awk瀏覽的檔名
FNR                瀏覽檔案的記錄數
FS                 設定輸入域分隔符,等價於命令列 -F選項
NF                 瀏覽記錄的域的個數
NR                 已讀的記錄數
OFS                輸出域分隔符
ORS                輸出記錄分隔符
RS                 控制記錄分隔符
本篇實驗資料如下:  ---從ORACLE啟動的ALERT日誌中取的一部分
[oracle@bys3 ~]$ cat awktest.log    --最後兩行手動增加了:號,方便實驗
MMAN started with pid=9, OS id=22862
DBW0 started with pid=10, OS id=22866
LGWR started with pid=11, OS id=22870
CKPT started with pid=12, OS id=22874
SMON:started with pid=13, OS id=22878     
RECO:started with pid=14, OS id=22882

說明---把個人覺得常用的寫了出來,關於AWK每個引數的沒寫,可以看DAVE的部落格:http://blog.csdn.net/tianlesoftware/article/details/6278273
##################################################################################################

輸出格式化及檔案合併、行列轉換等

awk中同時提供了print和printf兩種列印輸出的函式:
其中print函式的引數可以是變數、數值或者字串。字串必須用雙引號引用,引數用逗號分隔。如果沒有逗號,引數就串聯在一起而無法區分。這裡,逗號的作用與輸出檔案的分隔符的作用是一樣的,只是後者是空格而已。
printf函式,其用法和c語言中printf基本相似,可以格式化字串,輸出複雜時,printf更加好用,程式碼更易懂。
使用內建變數顯示輸入檔名,行號,列號,行的具體內容--filename如果是通過|管道傳來的資料,filename顯示為-
[oracle@bys3 ~]$ awk '{print "filename:" FILENAME ",linenumber:" NR ",columns:" NF ",linecontent:"$0}' awktest.log
filename:awktest.log,linenumber:1,columns:6,linecontent:MMAN started with pid=9, OS id=22862   --只顯示一行,後面行省略了
每2列轉換成一行
[oracle@bys3 ~]$ awk '{if (NR%2==0){print $0} else {printf"%s ",$0}}' awktest.log   
MMAN started with pid=9, OS id=22862  DBW0 started with pid=10, OS id=22866
每3行提取一行:
[oracle@bys3 ~]$ awk '(NR%3==0){print $0}' awktest.log
LGWR started with pid=11, OS id=22870
RECO:started with pid=14, OS id=22882
檔案的合併及拆分
[oracle@bys3 ~]$ cat awktest.log >awkt
[oracle@bys3 ~]$ awk '{print FILENAME,$0}' awktest.log awkt >a.log      --合併awktest.log awkt到a.log
[oracle@bys3 ~]$ cat a.log    --擷取了部分
awktest.log MMAN started with pid=9, OS id=22862
awktest.log DBW0 started with pid=10, OS id=22866
awkt MMAN started with pid=9, OS id=22862
awkt DBW0 started with pid=10, OS id=22866
[oracle@bys3 ~]$ rm -rf awkt*
將上一步合併後的檔案,拆分為合併前的兩個檔案。依照a.log第一列來產生新檔名
[oracle@bys3 ~]$ awk '$1!=fd{close(fd);fd=$1} {print substr($0,index($0," ")+1)>$1}' a.log
[oracle@bys3 ~]$ cat awkt
MMAN started with pid=9, OS id=22862
DBW0 started with pid=10, OS id=22866
LGWR started with pid=11, OS id=22870
CKPT started with pid=12, OS id=22874
SMON:started with pid=13, OS id=22878
RECO:started with pid=14, OS id=22882
[oracle@bys3 ~]$ cat awktest.log   --和cat awkt內容一樣
##################################################################################################

分隔符的使用示例:預設是空格或tab分隔

[oracle@bys3 ~]$ cat awktest.log |awk  '{print $5}'    --使用預設分割符
OS
OS
OS
OS
id=22878
id=22882
[oracle@bys3 ~]$ cat awktest.log |awk -F'[,=  :]' '{print $4" #@# "$5"\t"$9}'          --同時使用四個分隔符:,  =  : 空格    ;顯示第4、5、9個域,域之間用指定的符號分隔
pid #@# 9       22862
pid #@# 10      22866
pid #@# 11      22870
pid #@# 12      22874
pid #@# 13      22878
pid #@# 14      22882
##################################################################################################

同時使用'BEGIN END

BEGIN:讓使用者指定在第一條輸入記錄被處理之前所發生的動作,通常可在這裡設定全域性變數。
END:讓使用者在最後一條輸入記錄被讀取之後發生的動作。

awk工作流程是這樣的:先執行BEGING,然後讀取檔案,讀入有/n換行符分割的一條記錄,然後將記錄按指定的域分隔符劃分域,填充域,$0則表示所有域,$1表示第一個域,$n表示第n個域,隨後開始執行模式所對應的動作action。接著開始讀入第二條記錄······直到所有的記錄都讀完,最後執行END操作。
我這裡就是在BEGIN: END:時,顯示點字元,BEGIN/END可以只使用一個
[oracle@bys3 ~]$ cat awktest.log |awk -F'[,=  :]' 'BEGIN {print "header-a#@#bb     ospid"} {print $4"#@#"$5"\t"$9} END {print "Hello everone,my name is leifeng!"}'
header-a#@#bb     ospid
pid#@#9 22862
pid#@#10        22866
pid#@#11        22870
pid#@#12        22874
pid#@#13        22878
pid#@#14        22882
Hello everone,my name is leifeng!
##################################################################################################

過濾顯示需要的行:

首先使用'/MMAN/'過濾出包含MMAN的行,再傳入另一個AWK進行執行,\n換行,\t相當於TAB
[oracle@bys3 ~]$ cat awktest.log |awk -F '[,=  :]' '/MMAN/' |awk -F'[,=  :]' '{print $9"\n"$1"\t"$0}'
22862
MMAN    MMAN started with pid=9, OS id=22862
可以簡化為: 
[oracle@bys3 ~]$ cat awktest.log |awk -F '[,=  :]' '/MMAN/{print $9"\n"$1"\t"$0}'      --顯示行中有MMAN
22862
MMAN    MMAN started with pid=9, OS id=22862
顯示LGWR開頭的行到CKPT開頭的行--如果CKPT開頭的行後還有LGWR開頭的行,則繼續顯示到下一個CKPT開頭的行,如果不存在下一個CKPT開頭的行,則顯示到檔案結尾
[oracle@bys3 ~]$ cat awktest.log |awk '/^LGWR/,/^CKPT/'
LGWR started with pid=11, OS id=22870
CKPT started with pid=12, OS id=22874
[oracle@bys3 ~]$ cat awktest.log |awk '/^LGWR/,/^CKPq/'    --沒有CKPq開頭的行,一直顯示到檔案結尾
LGWR started with pid=11, OS id=22870
CKPT started with pid=12, OS id=22874
SMON:started with pid=13, OS id=22878
RECO:started with pid=14, OS id=22882
##################################################################################################

比較運算:可以做大於等於小與及加減除除運算

[oracle@bys3 ~]$ cat awktest.log |awk -F'[,=  :]' '{print $5}'  
9
10
11
12
13
14
[oracle@bys3 ~]$ cat awktest.log |awk -F'[,=  :]' '$5>12 {print $5}' 
13
14
[oracle@bys3 ~]$ cat awktest.log |awk -F'[,=  :]' '$5==10 {print $5 "#\t#" $0}'  
10#     #DBW0 started with pid=10, OS id=22866
[oracle@bys3 ~]$ cat awktest.log |awk -F'[,=  :]' '$5<10 {print $5 "\t" $0}'  
9       MMAN started with pid=9, OS id=22862
[oracle@bys3 ~]$ cat awktest.log |awk -F'[,=  :]' '$5<10 {print$5*9 "\t" $0}'       ---$5*9  顯示為9*9  --81
81      MMAN started with pid=9, OS id=22862
[oracle@bys3 ~]$ cat awktest.log |awk -F'[,=  :]' '$5<10 {print $5/9 "\t" $0}'    ---$5/9  9/9 --1
1       MMAN started with pid=9, OS id=22862
[oracle@bys3 ~]$ cat awktest.log |awk -F'[,=  :]' '$5<10 {print $5 "\t" $9}'  
9       22862
[oracle@bys3 ~]$ cat awktest.log |awk -F'[,=  :]' '$5*$9<220000 {print $5*$9 "###" $5 "\t" $9}'   --$5*$9<220000  顯示$5*$9小於22W的行,擷取此行的$5  $9
205758###9      22862
##################################################################################################

使用正規表示式部分匹配字元示例:

查詢開頭是MM的行
[oracle@bys3 ~]$ cat awktest.log |awk '/^MM/'
MMAN started with pid=9, OS id=22862
查詢開頭是MM或D或L的行
[oracle@bys3 ~]$ cat awktest.log |awk '/^(MM|D|L)/'
MMAN started with pid=9, OS id=22862
DBW0 started with pid=10, OS id=22866
LGWR started with pid=11, OS id=22870
查詢開關是字母M D L的行
[oracle@bys3 ~]$ cat awktest.log |awk '/^[MDL]/'
MMAN started with pid=9, OS id=22862
DBW0 started with pid=10, OS id=22866
LGWR started with pid=11, OS id=22870
查詢指定域中後兩位是數字,且數字分別是0-9 和0-2的   ~在這表示從結尾算
[oracle@bys3 ~]$ cat awktest.log |awk -F'[,=  :]' '$5 ~/[0-9][0-2]$/{print $5}'
10
11
12
[oracle@bys3 ~]$ cat awktest.log |awk -F'[,=  :]' '$5 ~/[3-4]$/{print $5}'    --查詢結尾字母是3-4的
13
14
##################################################################################################

正則邏輯式邏輯運算子:大於小於不等於 及 和&&  或|| 運算

[oracle@bys3 ~]$ cat awktest.log
MMAN started with pid=9, OS id=22862
DBW0 started with pid=10, OS id=22866
LGWR started with pid=11, OS id=22870
CKPT started with pid=12, OS id=22874
SMON:started with pid=13, OS id=22878
RECO:started with pid=14, OS id=22882
顯示$5==10||$9>22880  $5等於10或者$9>22880的行
[oracle@bys3 ~]$ cat awktest.log |awk -F'[,=  :]' '$5==10||$9>22880 {print $5"\t" $9}'  
10      22866
14      22882
顯示$5>10&&$9>22880  $5大於10並且$9>22880的行
[oracle@bys3 ~]$ cat awktest.log |awk -F'[,=  :]' '$5>10&&$9>22880 {print $5"\t" $9}'  
14      22882
顯示$5!=10 $5!=11  不等於10並且不等於11的行
[oracle@bys3 ~]$ cat awktest.log |awk -F'[,=  :]' '$5!=10&&$5!=11 {print $5"\t" $9}'
9       22862
12      22874
13      22878
14      22882
如果print ($5 > 12 ?  這裡如果判斷$5 > 12是否為真,真則顯示冒號前的值,如果不為真,則顯示冒號後值
[oracle@bys3 ~]$ cat awktest.log |awk -F'[,=  :]' '{print ($5 > 12 ? "ok \t"$5: "error\t"$5)}'
error   9
error   10
error   11
error   12
ok      13
ok      14

相關文章