文本處理三劍客linux
在 Shell 下使用這些正則表達式處理文本最多的命令有下面幾個工具:面試
命令 正則表達式 |
描述 編程 |
grepvim |
默認不支持擴展表達式,加-E 選項開啓 ERE。若是不加-E 使用花括號要加轉義符\{\}數組
|
egrep緩存 |
支持基礎和擴展表達式bash |
awkapp |
支持 egrep 全部的正則表達式less |
sed |
默認不支持擴展表達式,加-r 選項開啓 ERE。若是不加-r 使用花括號要加轉義符\{\}
|
sed詳解
咱們都知道,在Linux中一切皆文件,好比配置文件,日誌文件,啓動文件等等。若是咱們相對這些文件進行一些編輯查詢等操做時,咱們可能會想到一些vi,vim,cat,more等命令。可是這些命令效率不高,而在linux中有三種工具:頂配awk,中配sed,標配grep。使用這些工具,咱們可以在達到一樣效果的前提下節省大量的重複性工做,提升效率。
文件內容能夠是來自文件,也能夠直接來自鍵盤或者管道等標準輸入,最後的結果默認狀況下是顯示到終端的屏幕上,可是也能夠輸出到文件中。
編輯文件也是這樣,之前咱們修改一個配置文件,須要移動光標到某一行,而後添加點文字,而後又移動光標到另外一行,註釋點東西.......可能修改一個配置文件下來須要花費數十分鐘,還有可能改錯了配置文件,又得返工。這仍是一個配置文件,若是數十個數百個呢?所以當你學會了sed命令,你會發現利用它處理文件中的一系列修改是頗有用的。只要想到在大約100多個文件中,處理20個不一樣的編輯操做能夠在幾分鐘以內完成,你就會知道sed的強大了。
2. 語法格式
sed [選項] [sed命令] [輸入文件]
說明:
1,注意sed軟件以及後面選項,sed命令和輸入文件,每一個元素之間都至少有一個空格。
2,sed -commands(sed命令)是sed軟件內置的一些命令選項,爲了和前面的options(選項)區分,故稱爲sed命令
3,sed -commands 既能夠是單個sed命令,也能夠是多個sed命令組合。
4,input -file (輸入文件)是可選項,sed還可以從標準輸入如管道獲取輸入。
3. sed的工做原理
sed讀取一行,首先將這行放入到緩存中
而後,纔對這行進行處理
處理完成之後,將緩衝區的內容發送到終端
存儲sed讀取到的內容的緩存區空間稱之爲:模式空間(Pattern Space)
4. 選項說明
option[選項] |
解釋說明(帶*的爲重點) |
-n (no) |
取消默認的sed軟件的輸出,常與sed命令的p連用。* |
-e (entry) |
一行命令語句能夠執行多條sed命令 * |
-r (ruguler) |
使用擴展正則表達式,默認狀況sed只識別基本正則表達式 * |
-i (inside) |
直接修改文件內容,而不是輸出到終端,若是不使用-i選項sed軟件只是修改在內存中的數據,並不會影響磁盤上的文件* |
sed -commands[sed命令] |
解釋說明(帶*的爲重點) |
a (append) |
追加,在指定行後添加一行或多行文本 * |
c (change) |
取代指定的行 |
d (delete) |
刪除指定的行 * |
i (insert) |
插入,在指定行前添加一行或多行文本 * |
p (print) |
打印模式空間內容,一般p會與選項-n一塊兒使用* |
特殊符號 |
解釋說明(帶*的爲重點) |
! |
對指定行之外的全部行應用命令* |
sed增刪改查
1. 增
這裏咱們須要用到2個sed命令,分別是:
「a」:追加文本到指定行後,記憶方法:a的全拼是apend,意思是追加。
「i「:插入文本到指定行前,記憶方法:i的全拼是insert,意思是插入。
實例1:a [root@ken ~]# sed "2a 這是新添加的一行" test this is the first line this is the second line 這是新添加的一行 this is the third line this is the forth line this is the fivth line this is the sixth line this is the seventh line this is the eighth line this is the ninth line this is the tenth line 2表明指定對第2行操做,其餘的行忽略 a表明插入的意思,2i即在第2行前插入文本 2a後面加上空格,而後跟上你想要插入的文本便可 實例2: [root@ken ~]# sed "2i 我又新添加了一行" test this is the first line 我又新添加了一行 this is the second line this is the third line this is the forth line this is the fivth line this is the sixth line this is the seventh line this is the eighth line this is the ninth line this is the tenth line 實例3:同時增長多行(/n) [root@ken ~]# sed "2i 這是第一條記錄\n這是第二條記錄\n這是第三條記錄" test this is the first line 這是第一條記錄 這是第二條記錄 這是第三條記錄 this is the second line this is the third line this is the forth line this is the fivth line this is the sixth line this is the seventh line this is the eighth line this is the ninth line this is the tenth line
2.刪
這個功能也是很是得有用,好比咱們想刪除文件中的某些行,之前最經常使用的是vi或vim命令,但如今咱們知道了sed命令,就應該使用這個高逼格的命令完成任務了。
「d」:刪除文本,記憶方法:d的全拼是delete,意思是刪除。
sed軟件能夠對單行或多行文本進行處理。若是在sed命令前面不指定地址範圍,那麼默認會匹配全部行。
實例1:刪除全部的行 [root@ken ~]# cp test{,.bak} [root@ken ~]# sed 'd' test 命令說明:若是在sed命令前面不指定地址範圍,那麼默認會匹配全部行,而後使用d命令刪除功能就會刪除這個文件的全部內容 實例2:刪除指定的行 [root@ken ~]# cat test.bak >test [root@ken ~]# sed '2d' test this is the first line this is the third line this is the forth line this is the fivth line this is the sixth line this is the seventh line this is the eighth line this is the ninth line this is the tenth line 實例3:刪除指定範圍行 [root@ken ~]# sed '2,5d' test this is the first line this is the sixth line this is the seventh line this is the eighth line this is the ninth line this is the tenth line 實例4:刪除匹配的行 [root@ken ~]# sed '/sixth/d' test this is the first line this is the second line this is the third line this is the forth line this is the fivth line this is the seventh line this is the eighth line this is the ninth line this is the tenth line 命令說明:在sed軟件中,使用正則的格式和awk同樣,使用2個」/「包含指定的正則表達式,即「/正則表達式/」。 實例5:刪除指定行到行尾的內容 [root@ken ~]# sed '2,$d' test this is the first line 第二行也會被刪掉 實例6:取反 1、 [root@ken ~]# sed '2,3!d' test this is the second line this is the third line 2、 [root@ken ~]# sed '/tenth/!d' test this is the tenth line
3.改
「c」:用新行取代舊行,記憶方法:c的全拼是change,意思是替換。
[root@ken ~]# sed '2c 改過以後的第二行' test this is the first line 改過以後的第二行 this is the third line this is the forth line this is the fivth line this is the sixth line this is the seventh line this is the eighth line this is the ninth line this is the tenth line this is sixth line
文本替換
接下來講的這個功能,有工做經驗的同窗應該很是的熟悉,由於使用sed軟件80%的場景就是使用替換功能。
這裏用到的sed命令,選項:
「s」:單獨使用-->將每一行中第一處匹配的字符串進行替換==>sed命令
「g」:每一行進行所有替換-->sed命令s的替換標誌之一(全局替換),非sed命令。
「-i」:修改文件內容-->sed軟件的選項,注意和sed命令i區別。
sed軟件替換模型
sed -i 's/目標內容/替換內容/g' ken.log
sed -i 's#目標內容#替換內容#g'
實例1: [root@ken ~]# sed 's/line/hang/g' test this is the first hang this is the second hang this is the third hang this is the forth hang this is the fivth hang this is the sixth hang this is the seventh hang this is the eighth hang this is the ninth hang this is the tenth hang this is sixth hang 命令說明:從上面命令的結果咱們就知道sed命令默認不會修改文件的內容 實例2: [root@ken ~]# sed -i 's/line/hang/g' test [root@ken ~]# cat test this is the first hang this is the second hang this is the third hang this is the forth hang this is the fivth hang this is the sixth hang this is the seventh hang this is the eighth hang this is the ninth hang this is the tenth hang this is sixth hang 命令說明:若是想真正的修改文件內容,咱們就須要使用選項「-i」,這個要和sed命令「i」區分開來。同時咱們能夠發現命令執行後的結果是沒有任何輸出的。
4.查
這個功能也是很是得有用,好比咱們想查看文件中的某些行,之前最經常使用的是cat或more或less命令等,但這些命令有些缺點,就是不能查看指定的行。而咱們用了好久的sed命令就有了這個功能了。並且咱們前面也說過使用sed比其餘命令vim等讀取速度更快!
這裏咱們須要用到1個sed命令
「p」:輸出指定內容,但默認會輸出2次匹配的結果,所以使用-n選項取消默認輸出,記憶方法:p的全拼是print,意思是打印。
實例1: [root@ken ~]# sed '2p' test this is the first hang this is the second hang this is the second hang this is the third hang this is the forth hang this is the fivth hang this is the sixth hang this is the seventh hang this is the eighth hang this is the ninth hang this is the tenth hang this is sixth hang [root@ken ~]# sed -n '2p' test this is the second hang 實例2: [root@ken ~]# sed -n '2,5p' test this is the second hang this is the third hang this is the forth hang this is the fivth hang 實例3: [root@ken ~]# sed -n '/ninth/p' test this is the ninth hang 補充:-e多點操做 + 實例1: [root@ken ~]# sed -e '2d' -e '5d' test this is the first hang this is the third hang this is the forth hang this is the sixth hang this is the seventh hang this is the eighth hang this is the ninth hang this is the tenth hang this is sixth hang 實例2: [root@ken ~]# sed -n -e '2p' -e '5p' test this is the second hang this is the fivth hang
sed用法總結
1.查找指定的字符串 例子:顯示/etc/passwd中保含root的行(顯示模式空間中的內容) 方法1:set '/root/p' /etc/passwd 方法2:cat /etc/passwd | sed '/root/p' 2.在指定的位置作增刪 例子:刪除以root爲開頭的行 # sed '/^root/d' a.txt 例子:在包含root的行後添加一行 i am ken # sed '/root/a i am ken' a.txt 3.按行替換 例子:將5到9行的內容替換爲 i am ken # sed '5,9c i am ken' a.txt 4.按照字符替換 例子:將/etc/selinux/config中的SELINUX=enforcing改爲 disabled 寫法1:# sed -i 's/SELINUX=disabled/SELINUX=enforcing/g' config 寫法2:# sed -r -i 's/(SELINUX=)disabled/\1enforcing/g' config 5.查找指定的內容再作替換 例子:將以r開頭的行中的oo替換爲qq # sed '/^r/{s/oo/qq/g}' passwd 6.多點編輯 例子:去除文件中的註釋行和空白行 # grep -v -E "(^#)|(^$)" passwd.bak >passwd # cat passwd.bak | sed -e '/^#/d' -e '/^$/d' >passwd 7)取反操做 顯示非1-3行 # sed -n '1,3!p' passwd
awk詳解
awk不只僅時linux系統中的一個命令,並且是一種編程語言,能夠用來處理數據和生成報告(excel)。處理的數據能夠是一個或多個文件,能夠是來自標準輸入,也能夠經過管道獲取標準輸入,awk能夠在命令行上直接編輯命令進行操做,也能夠編寫成awk程序來進行更爲複雜的運用。
awk的格式
awk指令是由模式,動做,或者模式和動做的組合組成。
模式既pattern,能夠相似理解成sed的模式匹配,能夠由表達式組成,也能夠是兩個正斜槓之間的正則表達式。好比NR==1,這就是模式,能夠把他理解爲一個條件。
動做即action,是由在大括號裏面的一條或多條語句組成,語句之間使用分號隔開。好比awk使用格式:
awk處理的內容能夠來自標準輸入(<),一個或多個文本文件或管道。
pattern既模式,也能夠理解爲條件,也叫找誰,你找誰?高矮,胖瘦,男女?都是條件,既模式。
action既動做,能夠理解爲幹啥,找到人以後你要作什麼。
模式和動做的詳細介紹咱們放在後面部分,如今你們先對awk結構有一個瞭解。
awk參數
-F:指定分隔符
幾個小概念
記錄(record):一行就是一個記錄
分隔符(field separator):進行對記錄進行切割的時候所使用的字符
字段(field):將一條記錄分割成的每一段
FILENAME:當前處理文件的文件名
FS(Field Separator):字段分隔符(默認是以空格爲分隔符=)
NR(Number of Rrecord):記錄的編號(awk每讀取一行,NR就加1==) 【至關於行號】
NF(Number of Field):字段數量(記錄了當前這條記錄包含多少個字段==)
ORS(Output Record Separator):指定輸出記錄分隔符(指定在輸出結果中記錄末尾是什麼,默認是\n,也就是換行)
OFS(Output Field Separator):輸出字段分隔符
RS:記錄分隔符
輸出字段的表示方式
$1 $2 ... $n 輸出一個指定的字段
$NF 輸出最後一個字段
$0 輸出整條記錄
awk執行過程
[root@ken ~]# awk 'NR>=2&&NR<=5{print $0}' /etc/passwd bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
命令說明: 條件NR>=2,表示行號大於等於2時候,執行{print $0}顯示整行。 awk是經過一行一行的處理文件,這條命令中包含模式部分(條件)和動做部分(動做),awk將處理模式(條件)指定的行
1)awk讀入第一行內容
2)判斷是否符合模式中的條件NR>=2
a,若是匹配則執行對應的動做{print $0}
b,若是不匹配條件,繼續讀取下一行
3)繼續讀取下一行
4)重複過程1-3,直到讀取到最後一行(EOF:end of file)
準備測試文件 [root@ken ~]# head /etc/passwd > test [root@ken ~]# cat test root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin 實例1:打印行號 [root@ken ~]# awk '{print NR,$0}' test 1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologin 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin 4 adm:x:3:4:adm:/var/adm:/sbin/nologin 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6 sync:x:5:0:sync:/sbin:/bin/sync 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8 halt:x:7:0:halt:/sbin:/sbin/halt 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10 operator:x:11:0:operator:/root:/sbin/nologin 實例2:輸出有多餘5個字段的行的第三個字段 [root@ken ~]# awk -F ':' 'NF>=5{print $3}' test 0 1 2 3 4 5 6 7 8 11 實例3:輸出每行行號和該行有幾個字段 [root@ken ~]# awk -F ':' '{print NR,NF}' test 1 7 2 7 3 7 4 7 5 7 6 7 7 7 8 7 9 7 10 7
awk進階--正則
正則表達式的運用,默認是在行內查找匹配的字符串,如有匹配則執行action操做,可是有時候僅須要固定的列來匹配指定的正則表達式,好比:我想取/etc/passwd文件中第五列{$5}這一列查找匹配mail字符串的行,這樣就須要用另外兩個匹配操做符,而且awk裏面只有這兩個操做符來匹配正則表達式。
實例1:匹配整行 [root@ken ~]# awk '/^root/' test root:x:0:0:root:/root:/bin/bash 和下面的效果是同樣的 [root@ken ~]# awk '$0~/^root/' test root:x:0:0:root:/root:/bin/bash 注意:awk只用正則表達式的時候是默認匹配整行的即‘$0~/^root/’和‘/^root/’是同樣的。 實例2:匹配一行中的某一列 [root@ken ~]# awk -F ':' '$5~/root/' test root:x:0:0:root:/root:/bin/bash 提示: $5表示第五個區域(列) ~表示匹配(正則表達式匹配) /root/表示匹配root這個字符串 $5~/root/表示第五個區域(列)匹配正則表達式/root/,既第5列包含root這個字符串,則顯示這一行。 實例3:匹配行尾爲sync [root@ken ~]# awk -F ':' '/sync$/{print $0}' test sync:x:5:0:sync:/sbin:/bin/sync 實例4:顯示名字和登陸類型 [root@ken ~]# awk -F ':' '{print $1,$NF}' test root /bin/bash bin /sbin/nologin daemon /sbin/nologin adm /sbin/nologin lp /sbin/nologin sync /bin/sync shutdown /sbin/shutdown halt /sbin/halt mail /sbin/nologin operator /sbin/nologin $NF:表示匹配的末尾部分,這裏也能夠寫成$7
實戰: 取出網卡IP地址(企業面試題)
[root@ken ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000 link/ether 00:0c:29:99:ea:a6 brd ff:ff:ff:ff:ff:ff inet 172.20.10.6/24 brd 172.20.10.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet6 2408:84f4:86:47e1:20c:29ff:fe99:eaa6/64 scope global mngtmpaddr dynamic valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe99:eaa6/64 scope link valid_lft forever preferred_lft forever 第一種方法: [root@ken ~]# ip a | awk -F ' +' 'NR==9{print $3}' | awk -F '/' '{print $1}' 172.20.10.6 第二種方法: [root@ken ~]# ip a | grep -E '^ +.*inet\>.*' | awk -F ' +|/' 'NR==2{print $3}' 172.20.10.6 第三種方法: [root@ken ~]# hostname -i | awk -F ' ' '{print $3}' 172.20.10.6 第四種方法: [root@ken ~]# ip a | grep brd.*glo | awk -F ' +|/' '{print $3}' 172.20.10.6 第五種方法: [root@ken ~]# ip a | grep "scope" | awk 'NR==3{print $0}' | awk -F "( |/)+" '{print $3}' 172.20.10.6
awk特殊模式-BEGIN模式與END模式
BEGIN模塊再awk讀取文件以前就執行,通常用來定義咱們的內置變量(預約義變量,eg:FS,RS)
須要注意的是BEGIN模式後面要接跟一個action操做塊,包含在大括號內。awk必須在輸入文件進行任何處理前先執行BEGIN裏的動做(action)。咱們能夠不要任何輸入文件,就能夠對BEGIN模塊進行測試,由於awk須要先執行完BEGIN模式,纔對輸入文件作處理。BEGIN模式經常被用來修改內置變量ORS,RS,FS,OFS等值。
BEGIN模塊 實例1: [root@ken ~]# ifconfig eth0 | awk -F "[ :]+" 'NR==2{print $3}' 172.20.10.6 [root@ken ~]# ifconfig eth0 | awk -F "[^0-9.]+" 'NR==2{print $2}' 172.20.10.6 #上面的也能夠寫成 [root@ken ~]# ifconfig eth0 | awk 'BEGIN{FS="[ :]+"}NR==2{print $3}' 172.20.10.6 [root@ken ~]# ifconfig eth0 | awk 'BEGIN{FS="[^0-9.]+"}NR==2{print $2}' 172.20.10.6 實例2:在讀取文件以前,輸出些提示性信息(表頭)。 [root@ken ~]# awk -F ':' 'BEGIN{print "username","bash type"}{print $1,$NF}' test username bash type root /bin/bash bin /sbin/nologin daemon /sbin/nologin adm /sbin/nologin lp /sbin/nologin sync /bin/sync shutdown /sbin/shutdown halt /sbin/halt mail /sbin/nologin operator /sbin/nologin
END模塊
EHD在awk讀取完全部的文件的時候,再執行END模塊,通常用來輸出一個結果(累加,數組結果),也能夠是和BEGIN模塊相似的結尾標識信息
與BEGIN模式相對應的END模式,格式同樣,可是END模式僅在awk處理完全部輸入行後才進行處理。
實例1: [root@ken ~]# awk -F ':' 'BEGIN{print "username","bash type"}{print $1,$NF}END{print "end of file"}' test username bash type root /bin/bash bin /sbin/nologin daemon /sbin/nologin adm /sbin/nologin lp /sbin/nologin sync /bin/sync shutdown /sbin/shutdown halt /sbin/halt mail /sbin/nologin operator /sbin/nologin end of file 實例2:統計包含root的行的數量 方法一: [root@ken ~]# cat test | grep root| wc -l 2 方法二: [root@ken ~]# cat test | grep -c root 2 方法三: [root@ken ~]# cat /etc/passwd | awk 'BEGIN{i=0}/root/{i++}END{print i}' 2 [root@ken ~]# cat /etc/passwd | awk '/root/{i++}END{print i}' 2
總結awk執行過程
回顧一下awk的結構
awk -F 指定分隔符 ‘BRGIN{}END{}’,以下圖
awk數組
數組構成:
數組名[元素名]=值
如圖不難發現,awk數組就和酒店同樣。數組的名稱就像是酒店名稱,數組元素名稱就像酒店房間號碼,每一個數組元素裏面的內容就像是酒店房間裏面的人。
實戰:統計域名出現的次數(百度和搜狐面試題)
[root@ken ~]# cat test http://www.qq.com/ken http://www.qq.com/ken http://www.qq.com/ken http://www.qq.com/ken http://www.qq.com/ken http://www.qq.com/ken http://www.qq.com/ken http://www.sina.com/ken http://www.sina.com/ken http://www.sina.com/ken http://www.sina.com/ken http://www.sina.com/ken http://www.sina.com/ken http://www.sina.com/ken http://www.sina.com/ken http://www.sina.com/ken http://www.sina.com/ken http://www.sina.com/ken http://www.sina.com/ken http://www.sina.com/ken http://www.taobao.com/ken http://www.taobao.com/ken http://www.taobao.com/ken http://www.taobao.com/ken http://www.taobao.com/ken http://www.taobao.com/ken http://www.taobao.com/ken http://www.taobao.com/ken http://www.taobao.com/ken0 http://www.taobao.com/ken http://www.taobao.com/ken http://www.taobao.com/ken http://www.taobao.com/ken http://www.taobao.com/ken http://www.taobao.com/ken http://www.taobao.com/ken http://www.taobao.com/ken http://www.taobao.com/ken http://www.taobao.com/ken http://www.taobao.com/ken http://www.taobao.com/ken http://www.taobao.com/ken http://www.taobao.com/ken http://www.taobao.com/ken http://www.taobao.com/ken 方法一: [root@ken ~]# cat test | awk -F '/+' '{print $2}' | sort | uniq -c 7 www.qq.com 13 www.sina.com 25 www.taobao.com 方法二: [root@ken ~]# cat test | awk -F '/+' '{h[$2]++}END{for (i in h) print i,h[i]}' www.sina.com 13 www.qq.com 7 www.taobao.com 25
awk用法總結
1. 結合內置變量,打印指定的幾行,以及字段數量 例子;輸出有多餘5個字段的行的第三個字段 # cat a.sh | awk -F ":" 'NF>=5{print $3}' 例子:輸出每行行號和該行有幾個字段 # cat a.sh | awk -F ":" '{print NR,NF}' 例子:輸出用戶名,要求全部用戶顯示在同一行,並且用空格分隔 # cat mypwd | awk 'BEGIN{FS=":"; ORS=" "}{print $1}' 2. 結合正則來匹配一行或者某個字段 例子:輸出用戶名以s爲開頭的用戶的uid # cat mypwd | awk -F ":" '/^s/{print $}' 例子:輸出第五個字段是以t爲結尾的用戶的姓名 # cat mypwd | awk -F ":" '$5~/t$/{print $1}' 3. 採用比較符號來進行打印指定的某些行 例子:實現僅僅輸出3-5的內容,每行前面添加一個行號 # cat mypwd | awk 'NR>=3&&NR<=5{print NR,$1}' 或 # cat mypwd | awk 'NR==3,NR==5{print NR,$1}' 例子:實現僅僅輸出3 和 5 和 7行的內容,每行前面添加一個行號 # cat mypwd | awk 'NR==3||NR==5||NR==7{print NR,$1}' 4. END 例子:統計mypwd中以#開頭的行有多少行 # cat mypwd | awk 'BEGIN{n=0}/^#/{n+=1}END{print n}' 統計:mypwd中,以:爲分隔符,字段數量在3-5的行的數目 # cat mypwd | awk 'BEGIN{FS=":"}NF>=3&&NF<=5{n+=1}END{print n}' 5. ip 例子:統計IP [root@ken]# cat url.txt | awk -F "/+" '{urls[$2]++}END{for(key in urls)print key, urls[key]}’ www.baidu.com 12 haha.baidu.com 1 ftp.baidu.com 6 mail.baidu.com 7