溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用awk+sort+uniq進(jìn)行文本分析

發(fā)布時(shí)間:2020-08-06 22:57:07 來源:ITPUB博客 閱讀:305 作者:forest7707 欄目:建站服務(wù)器
原創(chuàng)作品,允許轉(zhuǎn)載,轉(zhuǎn)載時(shí)請務(wù)必以超鏈接形式標(biāo)明文章 原始出處 、作者信息和本聲明。否則將追究法律責(zé)任。http://wanyuetian.blog.51cto.com/3984643/1716971

1、uniq命令
uniq - report or omit repeated lines
介紹:uniq對指定的ASCII文件或標(biāo)準(zhǔn)輸入進(jìn)行唯一性檢查,以判斷文本文件中重復(fù)出現(xiàn)的行。常用于系統(tǒng)排查及日志分析
命令格式:
uniq [OPTION]... [File1 [File2]]
uniq從已經(jīng)排序好的文本文件File1中刪除重復(fù)的行,輸出到標(biāo)準(zhǔn)標(biāo)準(zhǔn)輸出或File2。常作為過濾器,配合管道使用。
在使用uniq命令之前,必須確保操作的文本文件已經(jīng)過sort排序,若不帶參數(shù)運(yùn)行uniq,將刪除重復(fù)的行。
常見參數(shù):
-c, --count              prefix  lines  by  the  number of occurrences 去重后計(jì)數(shù)
2、實(shí)戰(zhàn)演練

測試數(shù)據(jù):

1
2
3
4
5
6
7
8
[root@web01 ~]# cat uniq.txt 
10.0.0.9
10.0.0.8
10.0.0.7
10.0.0.7
10.0.0.8
10.0.0.8
10.0.0.9

a、直接接文件,不加任何參數(shù),只對相鄰的相同內(nèi)容去重:

1
2
3
4
5
6
[root@web01 ~]# uniq uniq.txt 
10.0.0.9
10.0.0.8
10.0.0.7
10.0.0.8
10.0.0.9

b、sort命令讓重復(fù)的行相鄰(-u參數(shù)也可完全去重),然后用uniq進(jìn)行完全去重

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@web01 ~]# sort uniq.txt 
10.0.0.7
10.0.0.7
10.0.0.8
10.0.0.8
10.0.0.8
10.0.0.9
10.0.0.9
[root@web01 ~]# sort -u uniq.txt 
10.0.0.7
10.0.0.8
10.0.0.9
[root@web01 ~]# sort uniq.txt|uniq
10.0.0.7
10.0.0.8
10.0.0.9

c、sort配合uniq去重后計(jì)數(shù)

1
2
3
4
[root@web01 ~]# sort uniq.txt|uniq -c
      2 10.0.0.7
      3 10.0.0.8
      2 10.0.0.9

3、企業(yè)案例
處理一下文件內(nèi)容,將域名取出并根據(jù)域名進(jìn)行計(jì)數(shù)排序處理(百度和sohu面試題)

1
2
3
4
5
6
7
[root@web01 ~]# cat access.log 
http://www.etiantian.org/index.html
http://www.etiantian.org/1.html
http://post.etiantian.org/index.html
http://mp3.etiantian.org/index.html
http://www.etiantian.org/3.html
http://post.etiantian.org/2.html

解答:
分析:此類問題是運(yùn)維工作中最常見的問題??梢匝葑兂煞治鋈罩荆榭碩CP各個(gè)狀態(tài)連接數(shù),查看單IP連接數(shù)排名等等。

1
2
3
4
[root@web01 ~]# awk -F '[/]+' '{print $2}' access.log|sort|uniq -c|sort -rn -k1
      3 www.etiantian.org
      2 post.etiantian.org
      1 mp3.etiantian.org
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI