文本處理工具下——分析文本的工具

 

 

 

 

一分析文本的工具

 

文本數據統計:wc
整理文本:sort
比較文件:diff 和patchcentos

 

 

 

 

(一)收集文本統計數據——wc


計數單詞總數、行總數、字節總數和字符總數,能夠對文件或STDIN 中的數據運行。bash

經常使用選項
-l 只計數行數
-w 只計數單詞總數
-c 只計數字節總數
-m 只計數字符總數
-L 顯示文件中最長行的長度併發

 

 

(1)顯示單詞總數、行總數、字節總數app

[root@centos72 ~]# wc  /app/passwd 19 27 841 /app/passwd [root@centos72 ~]# wc /app/f1 4 4 12 /app/f1 [root@centos72 ~]# ll /app/passwd -rw-r--r--. 1 root root 841 May 7 18:00 /app/passwd [root@centos72 ~]# ll /app/f1 -rw-r--r--. 1 root root 12 May 7 20:35 /app/f1

 

 

 

 

 

(2)wc結合管道ssh

wc和head同樣是交互式命令,讀取鍵盤輸入,那麼就能夠結合管道工具

只要是空格隔開的都是單詞post

[root@centos72 ~]# w  |  wc 5 41 336 [root@centos72 ~]# w 20:53:56 up 8:33, 3 users, load average: 0.16, 0.05, 0.06 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root tty1 13Jan19 8:12m 0.28s 0.28s -bash root pts/0 192.168.137.1 15:13 2:39m 0.10s 0.10s -bash root pts/1 192.168.137.1 12:41 4.00s 2.00s 0.02s w

 

 

 

 

 

 

[root@centos72 ~]# echo  0 1 2 3 > /app/f4 [root@centos72 ~]# /app/f4 | wc -bash: /app/f4: Permission denied 0 0 0 [root@centos72 ~]# cat /app/f4 | wc 1 4 8 [root@centos72 ~]# cat /app/f4 0 1 2 3 [root@centos72 ~]# echo 0 1 2 3 > /app/f5 [root@centos72 ~]# cat /app/f5 0 1 2 3 [root@centos72 ~]# cat /app/f5 | wc 1 4 8

 

 

 

 

 

(3)單獨統計行,單詞,字節,字符總數centos7

[root@centos72 ~]# cat  /app/passwd | wc -l 19 [root@centos72 ~]# cat /app/passwd | wc -w 27 [root@centos72 ~]# cat /app/passwd | wc -c 841 [root@centos72 ~]# cat /app/passwd | wc -m 841

 

 

 

 

 

注意字節和字符不同,字節是磁盤上佔用的空間。spa

一個漢字是一個字符,可是是好幾個字節日誌

字節是4個,字符是2個

[root@centos72 ~]# echo   我   > f1 [root@centos72 ~]#wc f1 1 1 4 f1 

 

 

 

 

[root@centos72 ~]# wc  -m f1 2 f1

 

 

 

 

使用二進制查看

[root@centos72 ~]# hexdump  -C  f2
00000000 e4 bd a0 0a |....| 00000004

 

 

 

 

查看多少人登陸系統

[root@centos72 ~]# who root tty1 2019-01-13 00:35 root pts/0 2019-05-07 15:13 (192.168.137.1) root pts/1 2019-05-07 12:41 (192.168.137.1) [root@centos72 ~]# who | wc -l 3

 

 

 

 

 

(4)-L 顯示文件中最長行的長度

[root@centos72 ~]# who root tty1 2019-01-13 00:35 root pts/0 2019-05-07 15:13 (192.168.137.1) root pts/1 2019-05-07 12:41 (192.168.137.1) [root@centos72 ~]# who | wc -l 3 [root@centos72 ~]# who | wc -L 54

 

 

 

 

 

 

 

 

(二)文本排序sort

 

把整理過的文本顯示在STDOUT ,不改變原始文件
sort [options] file(s)
經常使用選項
-r 執行反方向(由上至下)整理
-n 執行按數字大小整理
-f 選項忽略(fold )字符串中的字符大小寫
-u 選項(獨特,unique )刪除輸出中的重複行
-t  c 選項使用c 作爲字段界定符
-k X 選項按照使用c 字符分隔的X 列來整理可以使用屢次

 

 

(1)默認是讀取鍵盤輸入的,按照字符排序

分隔符是冒號,-k表示列

[root@centos72 ~]# sort  -t:   -k3  /app/passwd root:x:0:0:root:/root:/bin/bash wang:x:1000:1000:wang:/home/wang:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/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 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin polkitd:x:999:998:User for polkitd:/:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin

 

 

 

 

 

(2)-n 執行按數字大小整理

[root@centos72 ~]# sort -n  -t:   -k3  /app/passwd 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 games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin polkitd:x:999:998:User for polkitd:/:/sbin/nologin wang:x:1000:1000:wang:/home/wang:/bin/bash

 

 

 

 

 

 

 (3)-r 執行反方向(由上至下)整理

[root@centos72 ~]# sort   -nr   -t:   -k3  /app/passwd wang:x:1000:1000:wang:/home/wang:/bin/bash polkitd:x:999:998:User for polkitd:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin halt:x:7:0:halt:/sbin:/sbin/halt shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown sync:x:5:0:sync:/sbin:/bin/sync lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin root:x:0:0:root:/root:/bin/bash

 

 

 

 

 

[root@centos72 ~]# sort     -t:   -k1  /app/passwd adm:x:3:4:adm:/var/adm:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin halt:x:7:0:halt:/sbin:/sbin/halt lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin polkitd:x:999:998:User for polkitd:/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin root:x:0:0:root:/root:/bin/bash shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin wang:x:1000:1000:wang:/home/wang:/bin/bash

 

 

 

 

 

顯示用戶名和ID,而且按照ID的大小排序

[root@centos72 ~]# cut  -d:  -f1,3 /app/passwd | sort -nr -t: -k2 wang:1000 polkitd:999 systemd-network:192 nobody:99 postfix:89 dbus:81 sshd:74 ftp:14 games:12 operator:11 mail:8 halt:7 shutdown:6 sync:5 lp:4 adm:3 daemon:2 bin:1 root:0

 

 

 

 

 

建立一個文件

[root@centos72 ~]# cat  > f3 1 2 3 2 4 5 6 2 3 5 ^C^C [root@centos72 ~]# cat f3 1 2 3 2 4 5 6 2 3 5

 

 

 

 

 

 

默認按照字符順序排序的

[root@centos72 ~]# sort f3 1 2 2 2 3 3 4 5 5 6

 

 

 

 

 

 

(4) -u 選項(獨特,unique )刪除輸出中的重複行

刪除重複的數字

[root@centos72 ~]# sort  -u f3 1 2 3 4 5 6

 

 

 

 

[root@centos72 ~]# echo   11 >> f3 [root@centos72 ~]# echo 22 >> f3 [root@centos72 ~]# echo 33 >> f3 [root@centos72 ~]# cat f3 1 2 3 2 4 5 6 2 3 5 11 22 33 [root@centos72 ~]# sort -u f3 1 11 2 22 3 33 4 5 6

 

 

 

 

 

 

按照數字排序

[root@centos72 ~]# sort  -nu f3 1 2 3 4 5 6 11 22 33 [root@centos72 ~]# sort -un f3 1 2 3 4 5 6 11 22 33

 

 

 

 

 

 

(三)uniq


uniq 命令:從輸入中刪除先後相接的重複的行
uniq [OPTION]... [FILE]...
-c:  顯示每行重複出現的次數
-d:  僅顯示重複過的行
-u:  僅顯示未曾重複的行
連續且徹底相同方爲重複
常和sort  命令一塊兒配合使用:sort userlist.txt | uniq -c

 

 

建立文件

(1)從輸入中刪除先後相接的重複的行

[root@centos72 ~]# cat  > f4 a b a a bb bb c bb cc cc ^C [root@centos72 ~]# cat f4 a b a a bb bb c bb cc cc

 

 

 

 

[root@centos72 ~]# uniq f4 a b a bb c bb cc

 

 

 

 

 

(2)-c: 顯示每行重複出現的次數

[root@centos72 ~]# uniq -c f4 1 a 1 b 2 a 2 bb 1 c 1 bb 2 cc

 

 

 

 

 

(3)-u: 僅顯示未曾重複的行

[root@centos72 ~]# uniq -u f4 a b c bb

 

 

 

 

(4)-d: 僅顯示重複過的行

[root@centos72 ~]# uniq -d f4 a bb cc

 

 

 

 

(5)使用管道傳輸

空格做爲分隔符

[root@centos72 ~]# cut  -d" " -f1 /var/log/httpd/access_log 192.168.137.1 192.168.137.1 192.168.137.1 192.168.137.1 192.168.137.1 192.168.137.1 192.168.137.1 192.168.137.1 192.168.137.1 192.168.137.1 [root@centos72 ~]# cut -d" " -f1 /var/log/httpd/access_log | uniq 192.168.137.1

 

 

 

 

 

 

 

示例1:取出訪問日誌裏面訪問量最大或者前三的IP地址,而且從大到小排序

 

 

下面要啓動服務才能夠,首先要轉到快照。

若是沒有作快照那麼就只能把文件保存到電腦上

[root@centos72 ~]# cut  -d" " -f1 /var/log/httpd/access_log | sort -n | uniq -c | sort -nr | head | tr -s ' ' | cut -d " " -f3 192.168.137.1

 

 

 

 

 

 

 

遠程傳輸一個文件

[root@centos72 ~]# rz

[root@centos72 ~]# ls aaa aa.txt access_log anaconda-ks.cfg f1 f2 f3 f4 [root@centos72 ~]# ll -ht total 14M -rw-r--r--. 1 root root 25 May 7 22:12 f4 -rw-r--r--. 1 root root 30 May 7 22:04 f3 -rw-r--r--. 1 root root 4 May 7 21:08 f2 -rw-r--r--. 1 root root 4 May 7 21:03 f1 -rw-r--r--. 1 root root 27 May 7 19:11 aa.txt -rw-r--r--. 1 root root 9 May 7 13:28 aaa -rw-------. 1 root root 1.6K Jan 13 00:22 anaconda-ks.cfg -rw-r--r--. 1 root root 14M Dec 1 15:45 access_log [root@centos72 ~]# ll -hS total 14M -rw-r--r--. 1 root root 14M Dec 1 15:45 access_log -rw-------. 1 root root 1.6K Jan 13 00:22 anaconda-ks.cfg -rw-r--r--. 1 root root 30 May 7 22:04 f3 -rw-r--r--. 1 root root 27 May 7 19:11 aa.txt -rw-r--r--. 1 root root 25 May 7 22:12 f4 -rw-r--r--. 1 root root 9 May 7 13:28 aaa -rw-r--r--. 1 root root 4 May 7 21:03 f1 -rw-r--r--. 1 root root 4 May 7 21:08 f2

 

 

 

 

 

 

[root@centos72 ~]#  cut  -d" " -f1 access_log | sort -n | uniq -c | sort -nr 159091 172.18.56.3 4004 192.168.27.6 24 172.18.0.100

 

 

 

 

 

 若是訪問的主機數量不超過10個,那麼就能夠不加head

[root@centos72 ~]#  cut  -d" " -f1 access_log | sort -n | uniq -c | sort -nr | head 159091 172.18.56.3 4004 192.168.27.6 24 172.18.0.100

 

 

 

 

 

刪除多餘的空格

[root@centos72 ~]#  cut  -d" " -f1 access_log | sort -n | uniq -c | sort -nr | head | tr -s ' ' 159091 172.18.56.3 4004 192.168.27.6 24 172.18.0.100

 

 

 

 

 

以空格爲分隔符,取第3個字段

[root@centos72 ~]# cut  -d" " -f1 access_log | sort -n | uniq -c | sort -nr | head | tr -s ' ' | cut -d " " -f3 192.168.56.3 192.168.27.6 192.168.0.100

 

 

 

 

 

 

示例2:統計鏈接數


ss -nt 查詢併發鏈接的遠程IP最多的前三個IP

[root@centos72 ~]# ss -tn
State      Recv-Q Send-Q Local Address:Port Peer Address:Port ESTAB 0 52 192.168.137.72:22 192.168.137.1:57568 ESTAB 0 0 192.168.137.72:22 192.168.137.1:58228

 

 

 

 

 刪除多餘的空格,而且分割符替換爲冒號

[root@centos72 ~]# ss -tn  | grep  ESTAB |    tr -s ' ' : ESTAB:0:52:192.168.137.72:22:192.168.137.1:57568: ESTAB:0:0:192.168.137.72:22:192.168.137.1:58228:

 

 

 

 

 

以冒號做爲分隔符,取第6個字段的結果

[root@centos72 ~]# ss -tn  | grep  ESTAB |    tr -s ' ' : | cut -d: -f6 192.168.137.1 192.168.137.1

 

 

 

 

 

按照數字進行排序

[root@centos72 ~]# ss -tn  | grep  ESTAB |    tr -s ' ' : | cut -d: -f6 | sort -n 192.168.137.1 192.168.137.1

 

 

 

 

 

[root@centos72 ~]# ss -tn  | grep  ESTAB |    tr -s ' ' : | cut -d: -f6 | sort -n | uniq 192.168.137.1

 

 

 

 

 

顯示每行重複出現的次數

[root@centos72 ~]# ss -tn  | grep  ESTAB |    tr -s ' ' : | cut -d: -f6 | sort -n | uniq -c 2 192.168.137.1
相關文章
相關標籤/搜索