常見的文本處理工具node
在平常的linux運維工做當中,咱們常常要在一些文本當中抽取過濾出咱們所須要的信息,從而達到咱們的需求,須要特定的文本處理工具來幫咱們完成此類操做mysql
本章節主要講解的內容有:linux
文件查看:catgit
分頁查看文本:less、more正則表達式
抽取文件特定行數:head、tailsql
抽取文本特定列:cutshell
合併文本:pasteapache
文本統計:wcbash
文本排序並統計:sort、uniqless
比較文件:diff、patch
cat:連結查看文本和輸出至標準輸出
-n:爲每行添加行號
-A:顯示全部控制符
-E:爲每行顯示結束符$
-s:壓縮重複的空白行
-b:非空行進行編號
[root@CentOS6 ~]# cat -A test.txt #顯示全部控制字符 one$ two$ $ three$ $ [root@CentOS6 ~]# cat -E test.txt #在每行結尾添加$符 one$ two$ $ three$ $ [root@CentOS6 ~]# cat -b test.txt #非空行編號 1one 2two 3three [root@CentOS6 ~]# cat -n test.txt #顯示行號 1one 2two 3 4three 5 [root@CentOS6 ~]# cat -s test.txt #壓縮空白行 one two three [root@CentOS6 ~]#
nl:將文件每行添加行號顯示
tac:逆序顯示文件內容
rev:反向顯示文件內容
more: 分頁查看文件
more [OPTIONS...] FILE...
-d: 顯示翻頁及退出提示
less:一頁一頁查看文本文件
查看時有用的命令包括:
/文本搜索文本
n/N跳到下一個or 上一個匹配
head:抽取文本前n行
-n #:抽取文本前#行
-c #:抽取文本前#個字節7
-#:指定行數
tail:抽取文本後n行
-n #:抽取文本後#行
-#:指定行數
-c #:抽取文本後#個字節
-f:追蹤文本新添加的內容,不退出文本
[root@CentOS6 ~]# head -3 /etc/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 [root@CentOS6 ~]# tail -3 /etc/passwd #抽取文本的後三行 nologin:x:510:514::/home/nologin:/sbin/nologin mage:x:511:515::/home/mage:/bin/bash wang:x:512:516::/home/wang:/bin/bash [root@CentOS6 ~]# head -c 100 /etc/passwd #抽取文本前100個字節 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nol[root@CentOS6 ~]# [root@CentOS6 ~]# tail -c 100 /etc/passwd #抽取文本後100個字節 ome/nologin:/sbin/nologin mage:x:511:515::/home/mage:/bin/bash wang:x:512:516::/home/wang:/bin/bash [root@CentOS6 ~]#
cut:抽取文本
-d DELIMITER:指明分隔符,默認tab
-f FILEDS:
#:第#個字段
#,#:離散表示法,例如1,3,5
#-#:連續表示法,例如1-3
#,#-#,#:混合表示法例如,1,3-5,7
-c:按字符切割
--output-delimiter:指定輸出分隔符
[root@CentOS6 ~]# cat test.txt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin [root@CentOS6 ~]# cut -d: -f1,3 test.txt #以:爲分隔符,抽取文本的第1,3列 root:0 bin:1 daemon:2 [root@CentOS6 ~]#
paste:合併文本
-d DELIMITER:指定輸出分隔符
-s:全部行顯示爲一行
[root@CentOS6 ~]# paste issue test.txt #將兩個文本內容合併 CentOS release 6.8 (Final)root:x:0:0:root:/root:/bin/bash Kernel \r on an \mbin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin [root@CentOS6 ~]# paste -s issue test.txt #一個文本內容顯示爲一行 CentOS release 6.8 (Final)Kernel \r on an \m root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin [root@CentOS6 ~]#
wc:文本統計
-c:統計字節數
-l:統計行數
-w:統計單詞書
-m:統計字節數
[root@CentOS6 ~]# wc -l /etc/passwd #統計行數 47 /etc/passwd [root@CentOS6 ~]# wc -w /etc/passwd #統計單詞數 67 /etc/passwd [root@CentOS6 ~]# wc -m /etc/passwd #統計字節數 2143 /etc/passwd [root@CentOS6 ~]# wc -c /etc/passwd #統計字節數 2143 /etc/passwd [root@CentOS6 ~]#
sort:文本排序
-u:重複的行只出現一次
-r:逆序排序
-n:按數字從小到大排序
-f:忽略大小寫
-t DELIMITER:指明分隔符
-k #:按照指定的分隔符來指定列
[root@CentOS6 ~]# cat test.txt 1 1 2 1 3 [root@CentOS6 ~]# sort -u test.txt #重複的行只顯示一次 1 2 3 [root@CentOS6 ~]# sort -n test.txt #數字從小到大排序 1 1 1 2 3 [root@CentOS6 ~]# sort -r test.txt #逆序排序 3 2 1 1 1 [root@CentOS6 ~]# sort -t' ' -k1 -n test.txt #對指定的列排序 1 1 1 2 3 [root@CentOS6 ~]#
uniq:統計重複的行
-c:顯示重複行的重複次數
-d:僅顯示重複過的行
-u:顯示未曾重複的行
通常和sort命令搭配使用
[root@CentOS6 ~]# cat test.txt 1 1 2 1 3 [root@CentOS6 ~]# uniq -c test.txt #顯示重複行的重複次數 2 1 1 2 1 1 1 3 [root@CentOS6 ~]# uniq -d test.txt #僅顯示重複的行 1 [root@CentOS6 ~]# uniq -u test.txt #顯示未曾重複的行 2 1 3 [root@CentOS6 ~]#
diff:比較兩個文件的區別
以逐行的方式比較兩個文件的不一樣之處
diff /PATH/TO/OLDFILE /PATH/TO/NEWFILE > /PATH/TO/PATHCH_FILE
-u:使用unfied機制,即顯示要修改的行的上下文,默認爲三行
patch:向文件打補丁
path [OPTIONS] -i /PATH/TO/PATCH_FILE /PATH/TO/OLDFILE
patch /PATH/TO/OLDFILE < /PATH/TO/PATCH_FILE
使用以上命令來完成幾道練習題:
1.找出ifconfig命令中的IP地址
[root@CentOS6 ~]# ifconfig #命令執行結果 eth0 Link encap:Ethernet HWaddr 00:0C:29:52:81:65 inet addr:10.1.252.233 Bcast:10.1.255.255 Mask:255.255.0.0 inet6 addr: fe80::20c:29ff:fe52:8165/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:582651 errors:0 dropped:0 overruns:0 frame:0 TX packets:27749 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:60558505 (57.7 MiB) TX bytes:4391524 (4.1 MiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:192 errors:0 dropped:0 overruns:0 frame:0 TX packets:192 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:23756 (23.1 KiB) TX bytes:23756 (23.1 KiB) [root@CentOS6 ~]# ifconfig | head -2 #截取前兩行 eth0 Link encap:Ethernet HWaddr 00:0C:29:52:81:65 inet addr:10.1.252.233 Bcast:10.1.255.255 Mask:255.255.0.0 [root@CentOS6 ~]# ifconfig | head -2 | tail -1 #截取最後一行 inet addr:10.1.252.233 Bcast:10.1.255.255 Mask:255.255.0.0 [root@CentOS6 ~]# ifconfig | head -2 | tail -1 | cut -d: -f2 #以:爲分隔符,截取第二列 10.1.252.233 Bcast [root@CentOS6 ~]# ifconfig | head -2 | tail -1 | cut -d: -f2 | cut -d' ' -f1 #取出IP地址 10.1.252.233 [root@CentOS6 ~]#
2.查出分區空間使用率的最大百分比值
[root@CentOS6 ~]# df #命令輸出結果 Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda2 50264772 4350148 43354624 10% / tmpfs 502068 76 501992 1% /dev/shm /dev/sda1 194241 34110 149891 19% /boot /dev/sda3 20027260 44992 18958268 1% /testdir /dev/sr0 3824484 3824484 0 100% /media/CentOS_6.8_Final [root@CentOS6 ~]# df | tr -s ' ' #壓縮空格 Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda2 50264772 4350148 43354624 10% / tmpfs 502068 76 501992 1% /dev/shm /dev/sda1 194241 34110 149891 19% /boot /dev/sda3 20027260 44992 18958268 1% /testdir /dev/sr0 3824484 3824484 0 100% /media/CentOS_6.8_Final [root@CentOS6 ~]# df | tr -s ' ' | cut -d' ' -f5 #取出第五列 Use% 10% 1% 19% 1% 100% [root@CentOS6 ~]# df | tr -s ' ' | cut -d' ' -f5 | sort -n #數字從小到大排序 Use% 1% 1% 10% 19% 100% [root@CentOS6 ~]# df | tr -s ' ' | cut -d' ' -f5 | sort -n | tail -1 #取出百分比最大值 100% [root@CentOS6 ~]#
3.查出用戶UID最大值的用戶名、UID及shell類型
[root@CentOS6 ~]# sort -n -t: -k3 /etc/passwd #UID從小打到排序 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 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin apache:x:48:48:Apache:/var/www:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin tcpdump:x:72:72::/:/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 usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin pulse:x:497:495:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin saslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin rtkit:x:499:499:RealtimeKit:/proc:/sbin/nologin zhai:x:500:500::/home/zhai:/bin/bash hadoop:x:501:501::/home/hadoop:/bin/bash zaizai:x:502:503::/home/zaizai:/bin/bash alice:x:503:507::/home/alice:/bin/bash tom:x:504:508::/home/tom:/bin/bash user1:x:505:509::/home/user1:/bin/bash user2:x:506:510::/home/user2:/bin/bash bash:x:507:511::/home/bash:/bin/bash testbash:x:508:512::/home/testbash:/bin/bash basher:x:509:513::/home/basher:/bin/bash nologin:x:510:514::/home/nologin:/sbin/nologin mage:x:511:515::/home/mage:/bin/bash wang:x:512:516::/home/wang:/bin/bash nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin [root@CentOS6 ~]# sort -n -t: -k3 /etc/passwd | tail -1 #取出UID最大的用戶 nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin [root@CentOS6 ~]# sort -n -t: -k3 /etc/passwd | tail -1 | cut -d: -f1,3,7 #取出用戶名、UID、shell nfsnobody:65534:/sbin/nologin [root@CentOS6 ~]#
4.查出/tmp的權限,以數字方式顯示
[root@CentOS6 ~]# stat /tmp #命令執行結果 File: `/tmp' Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 802h/2050d Inode: 1835009 Links: 13 Access: (1777/drwxrwxrwt) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2016-08-05 11:54:12.000000000 +0800 Modify: 2016-08-06 07:45:28.201523630 +0800 Change: 2016-08-06 07:45:28.201523630 +0800 [root@CentOS6 ~]# stat /tmp | head -4 | tail -1 #取出包含權限的行 Access: (1777/drwxrwxrwt) Uid: ( 0/ root) Gid: ( 0/ root) [root@CentOS6 ~]# stat /tmp | head -4 | tail -1 | cut -d: -f2 #指定以:爲分隔符,取出第二列 (1777/drwxrwxrwt) Uid [root@CentOS6 ~]# stat /tmp | head -4 | tail -1 | cut -d: -f2 | tr -sc '[0-9]' '\n' #取出八進制權限 1777 [root@CentOS6 ~]#
5.統計當前鏈接本機的每一個遠程主機IP的鏈接數,並按從大到小排序
[root@CentOS6 ~]# netstat -tn #命令輸出結果 Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 10.1.252.233:36635 10.1.252.233:22 ESTABLISHED tcp 0 0 10.1.252.233:22 10.1.250.60:58479 ESTABLISHED tcp 0 0 10.1.252.233:22 10.1.252.84:40544 ESTABLISHED tcp 0 0 10.1.252.233:22 10.1.253.23:35810 ESTABLISHED tcp 0 0 10.1.252.233:22 10.1.252.84:40543 ESTABLISHED tcp 0 0 10.1.252.233:22 10.1.250.60:58179 ESTABLISHED tcp 0 0 10.1.252.233:22 10.1.252.233:36635 ESTABLISHED tcp 0 0 10.1.252.233:22 10.1.253.23:35811 ESTABLISHED tcp 0 0 10.1.252.233:22 10.1.250.60:61317 ESTABLISHED [root@CentOS6 ~]# netstat -tn | tr -s ' ' #壓縮空白字符 Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 10.1.252.233:36635 10.1.252.233:22 ESTABLISHED tcp 0 0 10.1.252.233:22 10.1.250.60:58479 ESTABLISHED tcp 0 0 10.1.252.233:22 10.1.252.84:40544 ESTABLISHED tcp 0 0 10.1.252.233:22 10.1.253.23:35810 ESTABLISHED tcp 0 0 10.1.252.233:22 10.1.252.84:40543 ESTABLISHED tcp 0 0 10.1.252.233:22 10.1.250.60:58179 ESTABLISHED tcp 0 0 10.1.252.233:22 10.1.252.233:36635 ESTABLISHED tcp 0 0 10.1.252.233:22 10.1.253.23:35811 ESTABLISHED tcp 0 64 10.1.252.233:22 10.1.250.60:61317 ESTABLISHED [root@CentOS6 ~]# netstat -tn | tr -s ' ' | cut -d' ' -f5 #截取第五列 servers) Address 10.1.252.233:22 10.1.250.60:58479 10.1.252.84:40544 10.1.253.23:35810 10.1.252.84:40543 10.1.250.60:58179 10.1.252.233:36635 10.1.253.23:35811 10.1.250.60:61317 [root@CentOS6 ~]# netstat -tn | tr -s ' ' | cut -d' ' -f5 | cut -d: -f1 #過濾掉端口號,只留IP地址 servers) Address 10.1.252.233 10.1.250.60 10.1.252.84 10.1.253.23 10.1.252.84 10.1.250.60 10.1.252.233 10.1.253.23 10.1.250.60 [root@CentOS6 ~]# netstat -tn | tr -s ' ' | cut -d' ' -f5 | cut -d: -f1 | tr -sc '[0-9].' '\n' #刪除IP地址之外的其餘字符 10.1.252.233 10.1.250.60 10.1.252.84 10.1.253.23 10.1.252.84 10.1.250.60 10.1.252.233 10.1.253.23 10.1.250.60 [root@CentOS6 ~]# netstat -tn | tr -s ' ' | cut -d' ' -f5 | cut -d: -f1 | tr -sc '[0-9].' '\n' | sort #將IP地址排序 10.1.250.60 10.1.250.60 10.1.250.60 10.1.252.233 10.1.252.233 10.1.252.84 10.1.252.84 10.1.253.23 10.1.253.23 [root@CentOS6 ~]# netstat -tn | tr -s ' ' | cut -d' ' -f5 | cut -d: -f1 | tr -sc '[0-9].' '\n' | sort | uniq -c #顯示重複行的重複次數 1 3 10.1.250.60 2 10.1.252.233 2 10.1.252.84 2 10.1.253.23 [root@CentOS6 ~]# netstat -tn | tr -s ' ' | cut -d' ' -f5 | cut -d: -f1 | tr -sc '[0-9].' '\n' | sort | uniq -c | sort -n -t' ' -k1 #將重複次數從小到大排序 1 2 10.1.252.233 2 10.1.252.84 2 10.1.253.23 3 10.1.250.60 [root@CentOS6 ~]#
正則表達式:Regular Expression
Linux上文本處理三劍客:
grep:文本過濾工具(模式:pattern)
grep:基本正則表達式,-E,-F
egrep:擴展正則表達式,-G,-F
fgrep:不支持正則表達式
sed:stream editor,流編輯器;文本編輯工具
awk:Linux上的實現爲gawk,文本報告生成器(格式化文本)
正則表達式:Regular Expression,REGEXP
由一類特殊字符文本字符所編寫的模式,其中有些字符不表示其字面意義,而是用於表示控制或統配的功能
分兩類:
基本正則表達式:BRE
擴展正則表達式:ERE
元字符:
grep:文本過濾工具
做用:文本搜索工具,根據用戶指定的「模式(過濾條件)」對目標文本逐行進行匹配檢查,打印匹配到的行
模式:由正則表達式的元字符及文本字符所編寫出的過濾條件
正則表達式引擎:
grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]
OPTIONS:
--color=auto:對匹配到的文本着色後高亮顯示
-i:在匹配時忽略大小寫
-o:只顯示匹配到的字符串
-v:顯示沒有被匹配到的內容
-E:使用擴展正則表達式
-q:靜默模式,不管匹配到或匹配不到都不輸出任何信息
-A #:顯示被匹配到的行的後#行
-B #:顯示被匹配到的行的前#行
-C #:顯示被匹配到的行的先後各#行
-e PAT1 -e PAR2:匹配PAT1或PAT2
-w:匹配完整單詞
-c:顯示匹配到的行數
-n:顯示匹配到的行號
基本正則表達式的元字符:
字符匹配:
.:匹配任意單個字符
[]:匹配指定範圍內任意單個字符
[^]:匹配指定範圍外任意單個字符
[:space:],[:digit:],[:alpha:],[:alnum:],[:punct:],[:upper:],[:lower:]
匹配次數:用在要指定其出現的次數的字符後面,用於限制其前面字符出現的次數,默認工做於貪婪模式
*:匹配其前面字符出現任意次
.*:表示匹配任意長度的任意字符
\?:匹配其前面字符出現1次或0次,(無關緊要)
\+:匹配其前面的字符至少出現1次
\{m\}:匹配其前面的字符m次
\{m,n\}:其匹配前一個字符至少m次,至多n次
\{m,\}:至少m次
\{0,n\}:至多n次
位置錨定:
^:其後面的字符必須出如今行首,用於模式的最左側
$:其後面的字符必須出如今行尾,用於模式的最右側
^PATTERN$:用於PATTERN來匹配整行
^$:匹配空白行
^[[:space:]]*$:匹配包含空格的空白行
單詞:非特殊字符組成的連續字符(字符串)都成爲單詞
\<或\b:錨定詞首,其後面的字符必須出如今詞首,用於單詞模式的左側
\>或\b:錨定詞尾,其後面的字符必須出如今詞尾,用於單詞模式的右側
\<PATTERN\>:匹配完整單詞
分組及引用:
\(\):將一個或多個字符捆綁子一塊兒,當作一個總體進行處理
note:分組括號中的模式匹配到的內容會被正則引擎自動記錄於內部的變量中,這些變量爲;
\1:模式從左側起,第一個左括號以及與之匹配的右括號之間的模式所匹配到的字符
\2:模式從左側起,第二個左括號以及與之匹配的右括號之間的模式所匹配到的字符
\3:
...
後向引用:引用前面的分組括號中的模式所匹配到的字符
egrep:
支持擴展的正則表達式實現相似於grep文本過濾功能:grep -E
grep [OPTIONS] PATTERN [FILE...]
選項:
-i, -o, -v, -q, -A, -B, -C
-G:支持基本正則表達式
擴展正則表達式的元字符:
字符匹配:
.:任意單個字符
[]:指定範圍內任意單個字符
[^]:指定範圍外的任意單個字符
次數匹配:
*:任意次,0、1或屢次
+:其前面的字符至少出現1次
?:其前面的字符出現0次或1次
{m}:其前面的字符出現m次
{m,n}:至少m次,至多n次
{0,n}:至多n次,至少不限
{m,}:至少m次,至多不限
位置錨定:
^:行首錨定
$:行尾錨定
\<,\b:詞首錨定
\>,\b:詞尾錨定
分組引用:
():分組;括號內的模式匹配到的字符會被記錄於正則表達式引擎的內部變量中
後向應用:\1,\2,\3,...
或:
a|b:a或者b
C|cat:C或cat
(c|C)at:cat或Cat