簡介
awk命令的名稱是取自三位創始人Alfred Aho 、Peter Weinberger 和 Brian Kernighan姓名的首字母,awk有自己的程序設計語言,設計簡短的程序,讀入文件,數據排序,處理數據,生成報表等功能。
awk 通常用於文本處理和報表生成,最基本功能是在文件或者字符串中基於指定規則瀏覽和抽取信息,awk抽取信息后,才能進行其他文本操作。
awk 通常以文件的一行為處理單位的,awk每接收文件的一行,然后執行相應的命令,來處理文本,完整的awk腳本通常用來格式化文本文件中的信息
使用方式
awk
'{pattern + action}'
{filenames}
pattern 表示 AWK 在數據中查找的內容,正則表達式,用斜杠括起來
action 是在找到匹配內容時所執行的一系列命令
花括號({})不需要在程序中始終出現,但它們用於根據特定的模式對一系列指令進行分組
使用說明
[hebinbin@iZ25y8wtfbqZ ~]$ awk '{print $0}' /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin
依次對/etc/passwd 中的每一行執行 print 命令,所有輸出都發送到 stdout,所得到的結果與執行 cat /etc/passwd 完全相同
$0 $n表示
-F參數:指定分隔符,可指定一個或多個
root@iZ25me8kko3Z:~# awk -F "/" -F ":" '{ print $1 " " $9 " " $0 }' /etc/passwd root root:x:0:0:root:/root:/bin/bash daemon daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin bin:x:2:2:bin:/bin:/bin/sh sys sys:x:3:3:sys:/dev:/bin/sh
只查看test.txt文件20-30行內容
root@iZ25me8kko3Z:~# awk '{if(NR>=20 && NR<=30) print $1}' test.txt sock.close() print('%s break elif data data #print(data) #print(clients) #if if print(data)
BEGIN 和 END 模塊
awk 在開始處理輸入文件之前會執行 BEGIN 塊,處理了輸入文件中的所有行之后執行END塊
統計/etc/passwd的賬戶人數
root@iZ25me8kko3Z:~# awk '{count++;print $0;} END{print "user count is ",count}' /etc/passwd root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh ................. user count is 47
count是自定義變量,沒有初始化默認是0,action{}中的多個語句用 ;隔開
root@iZ25me8kko3Z:~# awk 'BEGIN {count=0;print "[start] user count is ",count} {count=count+1;print $0} END{print "[end] user count is ",count}' /etc/passwd [start] user count is 0 root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh ..................... [end] user count is 47
統計某個文件夾下的文件占用的字節數
root@iZ25me8kko3Z:~# ll |awk 'BEGIN {size=0;} {size=size+$5;} END{print "[end]size is ",size}' [end]size is 3090190
awk運算符
root@iZ25me8kko3Z:~# awk 'BEGIN{a=5;a+=5;print a}' 10
root@iZ25me8kko3Z:~# awk 'BEGIN{a=1;b=2;print (a>2&&b>1,a=1||b>1)}'
0 1
root@iZ25me8kko3Z:~# awk 'BEGIN{a=1;b=2;print (a>2&&b>1,a=1||b>1)}' 0 1 root@iZ25me8kko3Z:~# awk 'BEGIN{a="100testaa";if(a~/100/) {print "ok"}}' ok root@iZ25me8kko3Z:~# echo|awk 'BEGIN{a="100testaaa"}a~/test/{print "ok"}' ok
root@iZ25me8kko3Z:~# awk 'BEGIN{a=11;if(a>=9){print "ok"}}' ok root@iZ25me8kko3Z:~# awk 'BEGIN{a;if(a>=b){print "ok"}}' ok root@
root@iZ25me8kko3Z:~# awk 'BEGIN{a="b";print a=="b"?"ok":"err"}' ok root@iZ25me8kko3Z:~# awk 'BEGIN{a="b";print a=="c"?"ok":"err"}' err
awk內置變量
awk正則使用
awk '/REG/{action} ' file,/REG/為正則表達式,可以將$0 中,滿足條件的記錄送入到:action 進行處理
root@iZ25me8kko3Z:/etc# awk '/root/{print $0}' passwd root:x:0:0:root:/root:/bin/bash
awk '布爾表達式{action}' file 僅當對前面的布爾表達式求值為真時, awk 才執行代碼塊
root@iZ25me8kko3Z:/etc# awk -F: '$1=="root"{print $0}' passwd root:x:0:0:root:/root:/bin/bash
awk 的 if、循環和數組
if條件語句
{ if ($1=="foo"){ if($2=="foo"){ print "uno" }else{ print "one" } }elseif($1=="bar"){ print "two" }else{ print "three" } }
循環結構
do...while
{ count=1do { print "I get printed at least once no matter what" } while ( count !=1 ) }
for
for ( initial assignment; comparison; increment ) { code block }
break continue
x=1 while(1) { print "iteration", x if ( x==10 ) { break } x++ }
x=1while (1) { if ( x==4 ) { x++ continue } print "iteration", x if ( x>20 ) { break } x++ }
數組 AWK 中的數組都是關聯數組,數字索引也會轉變為字符串索引
{ cities[1]=”beijing” cities[2]=”shanghai” cities[“three”]=”guangzhou” for( c in cities) { print cities[c] } print cities[1] print cities[“1”] print cities[“three”] }