在 Linux/UNIX
系統(tǒng)中,awk
是一個功能強大的編輯工具,逐行讀取輸入文本,并根據(jù)指定的匹配模式進行查找,對符合條件的內(nèi)容進行格式化輸出或者過濾處理,可以在無交互的情況下實現(xiàn)相當復(fù)雜的文本操作,被廣泛應(yīng)用于 Shell
腳本,完成各種自動化配置任務(wù)。
創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供南宮網(wǎng)站建設(shè)、南宮做網(wǎng)站、南宮網(wǎng)站設(shè)計、南宮網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、南宮企業(yè)網(wǎng)站模板建站服務(wù),十余年南宮做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
awk
所使用的命令格式如下所示,單引號加上大括號“{}”
用于設(shè)置對數(shù)據(jù)進行的處理動作。awk
可以直接處理目標文件,也可以通過“-f”
讀取腳本對目標文件進行處理。
awk 選項 '模式或條件 {編輯指令}' 文件1 文件2 //過濾并輸出文件符條件的內(nèi)容
awk -f 腳本文件 文件1 文件2 //從腳本中調(diào)用編輯指令,過濾并輸出內(nèi)容
sed
命令常用于一整行的處理,而 awk
比較傾向于將一行分成多個“字段”然后再進行處理,且默認情況下字段的分隔符為空格或者 tab
鍵。awk
執(zhí)行結(jié)果可以通過 print
的功能將字段數(shù)據(jù)打印顯示。在使用 awk
命令的過程中,可以使用邏輯操作符“&&”
,表示“與”, “||”
表示“或”,“!”
表示“非”;還可以進行簡單的數(shù)學(xué)運算,如+
、-
、*
、/
、%
、^
分別 表示加、減、乘、除、取余和乘方。
在 Linux 系統(tǒng)中/etc/passwd
是一個非常典型的格式化文件,各字段間使用“:”
作為分隔符隔開,Linux
系統(tǒng)中的大部分日志文件也是格式化文件,從這些文件中提取相關(guān)信息是運維的日常工作內(nèi)容之一。若需要查找出/etc/passwd
的用戶名、用戶 ID
、組 ID
等列, 執(zhí)行以下 awk 命令即可。
[root@localhost ~]# awk -F ':' '{print $1,$3,$4}' /etc/passwd
root 0 0
bin 1 1
daemon 2 2
adm 3 4
...//省略部分內(nèi)容...
//awk從輸入文件或者標準輸入中讀入信息,與 sed 一樣,信息的讀入也是逐行讀取的。不同的是awk將文本文件中的一行視為一個記錄,而將一行中的某一部分(列)作為記錄中的一個字段(域)。為了操作這些不同的字段,awk借用shell中類似于位置變量的方法, 用$1、$2、$3?順序地表示行(記錄)中的不同字段。另外awk用$0表示整個行(記錄)。不同的字段之間是通過指定的字符分隔。awk默認的分隔符是空格。awk允許在命令行中用“-F分隔符”的形式來指定分隔符。
awk
包含幾個特殊的內(nèi)建變量(可直接用)如下所示內(nèi)建變量 | 說明 |
---|---|
FS |
指定每行文本的字段分隔符,默認為空格或制表位 |
NF |
當前處理的行的字段個數(shù) |
NR |
當前處理的行的行號(序數(shù)) |
$0 |
當前處理的行的整行內(nèi)容。 |
$n |
當前處理行的第 n 個字段(第 n 列) |
FILENAME |
被處理的文件名 |
RS |
數(shù)據(jù)記錄分隔,默認為\n ,即每行為一條記錄 |
1)按行輸出文本
[root@localhost opt]# awk '{print}' bbb.txt //輸出所有內(nèi)容,等同于 cat bbb.txt
this is
the wood
wood
wod
the wood
this is test
[root@localhost opt]# awk '{print $0}' bbb.txt //輸出所有內(nèi)容,等同于 cat bbb.txt
this is
the wood
wood
wod
the wood
this is tes
[root@localhost opt]# awk 'NR==1,NR==3{print}' bbb.txt //輸出第 1~3 行內(nèi)容
this is
the wood
wood
[root@localhost opt]# awk '(NR>=1)&&(NR<=3){print}' bbb.txt //輸出第 1~3 行內(nèi)容
this is
the wood
wood
[root@localhost opt]# awk 'NR==1||NR==3{print}' bbb.txt //輸出第 1 行、第 3 行內(nèi)容
this is
wood
[root@localhost opt]# awk '(NR%2)==1{print}' bbb.txt //輸出所有奇數(shù)行的內(nèi)容
this is
wood
the wood
[root@localhost opt]# awk '(NR%2)==0{print}' bbb.txt //輸出所有偶數(shù)行的內(nèi)容
the wood
wod
this is test
[root@localhost opt]# awk '/^the/{print}' bbb.txt //輸出所有以the開頭的行
the wood
the wood
[root@localhost opt]# awk '/wood$/{print}' bbb.txt //輸出所有以wood結(jié)尾的行
the wood
wood
the wood
[root@localhost opt]# awk 'BEGIN {x=0};/\/bin\/bash$/{x++};END {print x}' /etc/passwd
2 //統(tǒng)計以/bin/bash 結(jié)尾的行數(shù),等同于 grep -c "/bin/bash$" /etc/passwd
[root@localhost opt]# awk 'BEGIN {RS=""};END{print NR}' httpd.txt //統(tǒng)計以空行分隔的文本段落數(shù)
38
2)按字段輸出文本
[root@localhost opt]# vim bbb.txt //編輯文本內(nèi)容
this is txt
the wood aaa
wood is bbb //編輯添加內(nèi)容
wod is ccc
the wood AAA
this is test
~
~
:wq //保存退出
[root@localhost opt]# awk '{print $3}' bbb.txt //輸出每行中(以空格或制表位分隔)的第 3 個字段
txt
aaa
bbb
ccc
AAA
test
[root@localhost opt]# awk '{print $1,$3}' bbb.txt //輸出每行中的第 1、3 個字段
this txt
the aaa
wood bbb
wod ccc
the AAA
this test
[root@localhost opt]# awk -F ":" '$2=="*"{print}' /etc/shadow //輸出密碼為“*”的用戶的shadow記錄
bin:*:17110:0:99999:7:::
daemon:*:17110:0:99999:7:::
adm:*:17110:0:99999:7:::
lp:*:17110:0:99999:7:::
sync:*:17110:0:99999:7:::
shutdown:*:17110:0:99999:7:::
halt:*:17110:0:99999:7:::
mail:*:17110:0:99999:7:::
operator:*:17110:0:99999:7:::
games:*:17110:0:99999:7:::
ftp:*:17110:0:99999:7:::
nobody:*:17110:0:99999:7:::
[root@localhost opt]# awk 'BEGIN{FS=":"};$2=="*";END{print}' /etc/shadow
bin:*:17110:0:99999:7::: //輸出密碼為“*”的用戶的shadow記錄
daemon:*:17110:0:99999:7:::
adm:*:17110:0:99999:7:::
lp:*:17110:0:99999:7:::
sync:*:17110:0:99999:7:::
shutdown:*:17110:0:99999:7:::
halt:*:17110:0:99999:7:::
mail:*:17110:0:99999:7:::
operator:*:17110:0:99999:7:::
games:*:17110:0:99999:7:::
ftp:*:17110:0:99999:7:::
nobody:*:17110:0:99999:7:::
named:!!:18178::::::
[root@localhost opt]# awk -F ":" '$7~"/bash"{print $1}' /etc/passwd
root //輸出以冒號分隔且第 7 個字段中包含/bash 的行的第 1 個字段
sun
[root@localhost opt]# awk '($1~"nfs")&&(NF==8){print $1,$2}' /etc/services
nfs 2049/tcp //輸出包含 8 個字段且第 1 個字段中包含 nfs 的行的第 1、2 個字段
nfs 2049/udp
nfs 2049/sctp
netconfsoaphttp 832/tcp
netconfsoaphttp 832/udp
netconfsoapbeep 833/tcp
netconfsoapbeep 833/udp
[root@localhost opt]# awk -F ":" '($7!="/bin/bash")&&($7!="/sbin/nologin"){print}' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync //輸出第 7 個字段既不為/bin/bash 也不為/sbin/nologin 的所有行
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
3)通過管道、雙引號調(diào)用 Shell 命令
[root@localhost opt]# awk -F : '/bash/{print | "wc -l"}' /etc/passwd
2 //調(diào)用wc -l 命令統(tǒng)計使用bash 的用戶個數(shù),等同于 grep -c "bash$" /etc/passwd
[root@localhost opt]# awk 'BEGIN {while ("w" | getline) n++ ; {print n-2}}'
1 //調(diào)用w 命令,并用來統(tǒng)計在線用戶數(shù),使用while循環(huán)匹配w 命令輸出行數(shù),getline為只顯示行數(shù),n-2為減去前兩行
[root@localhost opt]# awk 'BEGIN { "hostname" | getline ; print $0}'
localhost.localdomain //調(diào)用hostname,并輸出當前的主機名
本文題目:Shell編程之正則表達式(三)
網(wǎng)站URL:http://m.kartarina.com/article24/jedsje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、搜索引擎優(yōu)化、響應(yīng)式網(wǎng)站、網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)