測試開發(fā)實戰(zhàn) | 測試必會之 Linux 三劍客 ( grep / awk / sed )
測試開發(fā)實戰(zhàn) | 測試必會之 Linux 三劍客 ( grep / awk / sed )

Linux 給人的印象是黑乎乎的神秘窗口,文本操作和數(shù)據(jù)處理似乎沒有 Windows 窗口界面直觀方便。其實Linux 有自己的獨特的法寶,稱之為?三劍客:grep,awk 和 sed?。你可以用這三件法寶很方便的處理數(shù)據(jù) :?查找,分段,修改?,而這三個功能對應著我們今天的主角:grep,awk,sed。
形象一點比喻,如果把數(shù)據(jù)比作人群,那么?grep 就是照妖鏡?,用來找出妖精;?awk 就是尺子?,?給人群分門別類;而?sed 就是寶劍?,用來除掉妖精。當你明白為什么要用三劍客時,就更容易拿這三把劍去斬妖除魔。
1. grep

grep-global regular expression print - 全局正則表達式打印
可用于數(shù)據(jù)查找定位
先列舉出測試工作常用的grep命令和意義:
grep pattern file
grep -i pattern file 忽略大小寫
grep -v pattern file ?不顯示匹配行
grep -o pattern file 只把每個匹配的內容獨立的行顯示
grep -E pattern file 使用拓展正則表達式
#注意:grep 'a[0-9]\{10\}' 等同于 grep -E 'a[0-9]{10}'
grep -A -B -C pattern file 打印命中數(shù)據(jù)的上下文
grep pattern -r dir/ 遞歸搜索
grep -m1 匹配匹配中的第一個
grep -n 順便輸出行號
下面以一個檢查首頁是否有死鏈的案例需求來展示 grep 的匹配用法
以目前國內最大的測試社區(qū)網(wǎng)站 testerhome 為例,訪問 testerhome 主頁,找出主頁中包含的左右 url,分別進行訪問,如果訪問成功會返回狀態(tài)碼200,檢查所有訪問成功的url并打印出來,若沒訪問成功就打印ERR加上失敗的url。
先訪問 Testerhome 社區(qū)主頁,利用 grep href 過濾出所有包含 url 的內容。命令:
curl -s https://testerhome.com | grep href

2.從返回的結果中取出 url,觀察發(fā)現(xiàn)所有的 url 都被包在了雙引號之中,那么在利用 grep -o 命令,加上正則表達式匹配,只打印從 http 開始到 url 結束雙引號之前的內容。命令:
curl -s https://testerhome.com | grep href | grep -o "http[^\"]*"

從上一步中我們已經(jīng)取出了完整的 url 了,現(xiàn)在我們需要對每個url進行訪問取值判斷
3.1. 先用curl -I 看看請求返回的頭信息內容。命令:
curl -s -I https://testerhome.com/topics/feed

3.2. 訪問成功返回200,這時候我們一行一行去訪問,再用grep命令匹配"200 OK"作為判斷條件,篩選出成功的url并打印,然后將失敗的 url 加上 ERR 標記也一起打印出來。命令
curl -s https://testerhome.com | grep href | grep -o "http[^\"]*" | while read line;do curl -s -I $line | grep 200 && echo $line || echo ERR $line;done
4.最終結果展示

2. awk

awk = “Aho Weiberger and Kernighan” 三個作者的姓的第一個字母
awk 是 Linux 下的一個命令,同時也是一種語言解析引擎
awk 具備完整的編程特性。比如執(zhí)行命令,網(wǎng)絡請求等
精通 awk,是一個 Linux 工作者的必備技能
語法:awk ‘pattern{action}’
awk pattern語法
awk 理論上可以代替 grep
awk ‘pattern{action}’ ,默認以空格分隔
awk ‘BBEGIN{}END{}’ 開始和結束
awk ‘/Running/’ 正則匹配
awk ‘/aa/,/bb/’ 區(qū)間選擇
awk ‘$2~/xxx/’ 字段匹配,這里指從第2個字段開始匹配包含xxx內容的行
awk ’NR==2’ 取第二行
awk ’NR>1’ 去掉第一行
awk的字段數(shù)據(jù)處理
-F 參數(shù)指定字段分隔符
BEGIN{FS=‘_’} 也可以表示分隔符
$0 代表原來的行
$1 代表第一個字段
$N 代表第N個字段
$NF 代表最后一個字段
下面以一個在nginx.log中查找返回狀態(tài)碼非200的請求響應數(shù)目的需求為例,演示awk的基礎用法
有一份nginx.log文件,打開后內容格式如下:
220.181.108.111 - - [05/Dec/2018:00:11:42 +0000] "GET /topics/15225/show_wechat HTTP/1.1" 200 1684 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)" 0.029 0.029 .
216.244.66.241 - - [05/Dec/2018:00:11:42 +0000] "GET /topics/10052/replies/85845/reply_suggest HTTP/1.1" 301 5 "-" "Mozilla/5.0 (compatible; DotBot/1.1; http://www.opensiteexplorer.org/dotbot, help@moz.com)" 0.016 0.016 .
216.244.66.241 - - [05/Dec/2018:00:11:42 +0000] "GET /topics/10040?order_by=created_at HTTP/1.1" 301 5 "-" "Mozilla/5.0 (compatible; DotBot/1.1; http://www.opensiteexplorer.org/dotbot, help@moz.com)" 0.002 0.002 .
216.244.66.241 - - [05/Dec/2018:00:11:42 +0000] "GET /topics/10043/replies/85544/reply_suggest HTTP/1.1" 301 5 "-" "Mozilla/5.0 (compatible; DotBot/1.1; http://www.opensiteexplorer.org/dotbot, help@moz.com)" 0.001 0.001 .
216.244.66.241 - - [05/Dec/2018:00:11:44 +0000] "GET /topics/10075/replies/89029/edit HTTP/1.1" 301 5 "-" "Mozilla/5.0 (compatible; DotBot/1.1; http://www.opensiteexplorer.org/dotbot, help@moz.com)" 0.001 0.001 .
216.244.66.241 - - [05/Dec/2018:00:11:44 +0000] "GET /topics/10075/replies/89631/edit HTTP/1.1" 301 5 "-" "Mozilla/5.0 (compatible; DotBot/1.1; http://www.opensiteexplorer.org/dotbot, help@moz.com)" 0.001 0.001 .
216.244.66.241 - - [05/Dec/2018:00:11:45 +0000] "GET /topics/10075?order_by=created_at HTTP/1.1" 301 5 "-" "Mozilla/5.0 (compatible; DotBot/1.1; http://www.opensiteexplorer.org/dotbot, help@moz.com)" 0.000 0.000 .
216.244.66.241 - - [05/Dec/2018:00:11:45 +0000] "GET /topics/10075?order_by=like HTTP/1.1" 301 5 "-" "Mozilla/5.0 (compatible; DotBot/1.1; http://www.opensiteexplorer.org/dotbot, help@moz.com)" 0.001 0.001 .
223.71.41.98 - - [05/Dec/2018:00:11:46 +0000] "GET /cable HTTP/1.1" 101 60749 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0" 2608.898 2608.898 .
113.87.161.17 - - [05/Dec/2018:00:11:39 +0000] "GET /cable HTTP/1.1" 101 3038 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36" 112.418 112.418 .
216.244.66.241 - - [05/Dec/2018:00:11:46 +0000] "GET /topics/10079/replies/119591/edit HTTP/1.1" 301 5 "-" "Mozilla/5.0 (compatible; DotBot/1.1; http://www.opensiteexplorer.org/dotbot, help@moz.com)" 0.001 0.001 .
216.244.66.241 - - [05/Dec/2018:00:11:46 +0000] "GET /topics/10089?locale=zh-TW HTTP/1.1" 301 5 "-" "Mozilla/5.0 (compatible; DotBot/1.1; http://www.opensiteexplorer.org/dotbot, help@moz.com)" 0.002 0.002 .
觀察log內容,可以發(fā)現(xiàn),以空格為分隔符,狀態(tài)碼在第九個字段位置;這里我們用awk命令從第九個字段位置開始匹配非200的狀態(tài)碼并打印出來。命令:
awk '$9!~/200/{print $9}' nginx.log
[avbxb9efockpz ~]$ awk '$9!~/200/{print $9}' nginx.log301
301
301
301
301
301
301
301
301
......#剩余部分省略
再對取出的數(shù)據(jù)進行排序->去重->按數(shù)字的倒敘進行排列。命令:
awk '$9!~/200/{print $9}' nginx.log | sort | uniq -c | sort -nr
命令含義:
sort: 按從小到大進行排序
uniq -c :去重(相鄰)
-nr: 按數(shù)字進行倒敘排序
-n:按數(shù)字進行排序
結果展示:
[sqavbxb9efockpz ~]$ awk '$9!~/200/{print $9}' nginx.log | sort | uniq -c | sort -nr
433 101
304 301
? ?
266 404
? ?
152 302
? ? ?
7 401
? ? ?
5 304
? ? ?
2 499
? ? ?
2 422
? ??
1 500
再結合 awk ‘BBEGIN{}END{}’ 命令,以統(tǒng)計當前用戶數(shù)目的例子來展示命令用法
使用?cat /etc/passwd
?命令來查看本機用戶,我們需要提取出用戶名稱并加上數(shù)字序號顯示出來,達到這種效果:
1 nobody
2 root
3 daemon
4 _uucp
5 _taskgated
6 _networkd
7 _installassistant
8 _lp
9 _postfix
......
用戶信息:
localhost:~ qinzhen$ cat /etc/passwd
##
# User Database
#
# Note that this file is consulted directly only when the system is running
# in single-user mode. ?At other times this information is provided by
# Open Directory.
#
# See the opendirectoryd(8) man page for additional information about
# Open Directory.
##
nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false
_uucp:*:4:4:Unix to Unix Copy Protocol:/var/spool/uucp:/usr/sbin/uucico
_taskgated:*:13:13:Task Gate Daemon:/var/empty:/usr/bin/false
_networkd:*:24:24:Network Services:/var/networkd:/usr/bin/false
_installassistant:*:25:25:Install Assistant:/var/empty:/usr/bin/false
_lp:*:26:26:Printing Services:/var/spool/cups:/usr/bin/false
_postfix:*:27:27:Postfix Mail Server:/var/spool/postfix:/usr/bin/false
_scsd:*:31:31:Service Configuration Service:/var/empty:/usr/bin/false
_ces:*:32:32:Certificate Enrollment Service:/var/empty:/usr/bin/false
_appstore:*:33:33:Mac App Store Service:/var/empty:/usr/bin/false
_mcxalr:*:54:54:MCX AppLaunch:/var/empty:/usr/bin/false
......#后面的省略
思路:
* awk運行前先定義序號索引0,用來遞增保存用戶
* 利用awk將用戶提取出來,按索引分別保存;
* 切片結束后再按行數(shù)進行循環(huán),將數(shù)字序號與第一步保存的信息拼接打印
* 注意:
cat /etc/passwd
打印出的結果中,最上方的注釋需要處理跳過
cat /etc/passwd | awk -F ':' 'BEGINE{userindex=0}{user[userindex]=$1;userindex++}END{for(i=0;i<NR;i++)print i+1, user[i+10]}' |less

3. sed

sed:stream editor 根據(jù)定位到的數(shù)據(jù)行修改數(shù)據(jù)
sed [-nefri] [動作]
參數(shù):
-n :使用安靜(slient)模式。只有經(jīng)過sed特殊處理的那一行(或者操作)才會被列出來。一般與p配合使用
-e :直接在命令行模式上進行sed的動作編輯
-f :直接將sed動作寫在一個文件內,
-f filename則可以執(zhí)行filename 內的sed動作。
-r :sed的動作支持的是拓展正則表達式的語法(默認是基礎正則表達式的語法)
-i :直接修改讀取的文件內容,而不是由屏幕輸出
動作說明:[[n1][,n2]]function
n1,n2 :不見得會存在,一般代表選擇進行動作的行數(shù),舉例來說,如果我的動作是需要在10到20之間進行的,則“10,20[動作行為]”
function有下面這些參數(shù):
a :新增
d :刪除 (比較重要,測試工作中對數(shù)據(jù)處理時可快速去除無用信息,比如注釋行,空白行等)
i :插入
p :打印 (一般與-n配合使用)
s :替換(重中之重?。?!,s參數(shù)可以說是日常測試工作中對數(shù)據(jù)用sed清理過濾時使用率最高的了)
sed 修改表達式:?sed 's/待修改/修改結果/'
注意說明:
表達式單引號中的s表示修改,/ 符號表示分隔,實際上將/換成其他符號也可以,只要能起到分隔作用就OK
[16210504@izuf60jasqavbxb9efockpz ~]$ echo "aaa|bbb}|cccbbb" | sed 's/bbb/BBB/'
aaa|BBB}|cccbbb
[16210504@izuf60jasqavbxb9efockpz ~]$ echo "aaa|bbb}|cccbbb" | sed 's#bbb#BBB#'
aaa|BBB}|cccbbb
aaa|BBB}|cccbbb
若想講目標中所有的字段都替換,需要在命令最后加上g:
[16210504@izuf60jasqavbxb9efockpz ~]$ echo "aaa|bbb}|cccbbb" | sed 's/bbb/BBB/g'
aaa|BBB}|cccBBB
sed還可以修改文件中的內容,現(xiàn)在有文件text.txt,內容如下:
[16210504@izuf60jasqavbxb9efockpz ~]$ cat text.txt
hello bash world
hi~ tester
go go go go!
用?sed 's/hello/HELLO/' text.txt
?命令將文件中的?hello
?替換成?HELLO
?:
[16210504@izuf60jasqavbxb9efockpz ~]$ sed 's/hello/HELLO/' text.txt
HELLO bash world
hi~ tester
go go go go!
但是此時我們打開源text.txt文件發(fā)下源文件內容并未改變:
[16210504@izuf60jasqavbxb9efockpz ~]$ cat text.txt
hello bash world
hi~ tester
go go go go!
注意說明:
sed 在修改文件內容時,是另外開辟了一塊模式空間,將修改后的內容放入并輸出,源文件并未修改;
這時如果想要修改源文件就需要借助?-i
?命令,另外為了防止誤操作修改文件,一般可以采取這種寫法:?sed -i.bak 's/hello/HELLO/' text.txt
?,這種寫法在修改源文件的同時還會生成一份以.bak結尾的備份文件,相較安全。
[16210504@izuf60jasqavbxb9efockpz ~]$ sed -i.bak 's/hello/HELLO/' text.txt
[16210504@izuf60jasqavbxb9efockpz ~]$ ls
1 ?1.sh ?Allen_qin ?nginx.log ?test ?text.txt ?text.txt.bak ?while_test
[16210504@izuf60jasqavbxb9efockpz ~]$ cat text.txt
HELLO bash world
hi~ tester
go go go go!
[16210504@izuf60jasqavbxb9efockpz ~]$ cat text.txt.bak
hello bash world
hi~ tester
go go go go!
sed -e
?命令可以直接在命令行模式上進行sed的動作編輯,但看解釋比較晦澀,來看一個實例:
需求:?現(xiàn)有一個1.txt的文本,內容如下:
a:
b:
c:
d:
要將其中每行末尾的?
:
?都替換成?@
?,將?a
?替換成?A
?,并在文本末尾加上“?Sed Test
?”
命令:
sed -i -e 's/:/@/g' \
-i -e 's/a/A/' \
-i -e '$a Sed Test' 1.txt
實例演示:
[16210504@izuf60jasqavbxb9efockpz ~]$ sed -i -e 's/:/@/g' -i -e 's/a/A/' -i -e '$a Sed Test' 1.txt
[16210504@izuf60jasqavbxb9efockpz ~]$ cat 1.txt
A@
b@
c@
d@
Sed Test