1 常見文本處理工具
cat
- -E:顯示行結束符$
- -A:顯示全部控制符
- -n:對顯示出的每一行進行編號
- -b:非空行編號
- -s:壓縮連續的空行成一行
[root@localhost ~]# cat a -A $ localhost.localdomain$ a b c$ a ^I^I d$ [root@localhost ~]# cat a -E $ localhost.localdomain$ a b c$ a d$
[root@localhost ~]# cat a -n
1
2 localhost.localdomain
3 a b c
4 a d
[root@localhost ~]# cat a -b
nginx
1 localhost.localdomain 2 a b c 3 a d 4 c
### nl 至關於cat -b
[root@localhost ~]# nl b
1 a
2 b
3 c
4 b
5 e
6 f
shell
### tac 逆向顯示文本內容
[root@localhost ~]# tac b
f
e
b
c
b
a
centos
### rev 將同一行的內容逆向顯示
[root@localhost ~]# echo {1..10} | rev
01 9 8 7 6 5 4 3 2 1
bash
### hexdump 查看非文本文件內容
[root@localhost ~]# hexdump /dev/sda -c -n 512less
### od od 即 dump fifiles in octal and other formats
[root@localhost ~]# echo {a..z} | tr -d ' ' | od -t x1z
0000000 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 >abcdefghijklmnop<
0000020 71 72 73 74 75 76 77 78 79 7a 0a >qrstuvwxyz.<
0000033
dom
echo {a..z} | tr -d ' '|xxd
0000000: 6162 6364 6566 6768 696a 6b6c 6d6e 6f70 abcdefghijklmnop
0000010: 7172 7374 7576 7778 797a 0a qrstuvwxyz.
ssh
### more 能夠實現分頁查看文件,能夠配合管道實現輸出信息的分頁 ### less less 命令是man命令使用的分頁器 ### head 能夠顯示文件或標準輸入的前面行 * -c # 指定獲取前#字節 * -n # 指定獲取前#行 * -# 同上 ### tail tail 和head 相反,查看文件或標準輸入的倒數行 * -c # 指定獲取後#字節 * -n # 指定獲取後#行 * -# 同上 * -f 跟蹤顯示文件fd新追加的內容,經常使用日誌監控,至關於 --follow=descriptor,當文件刪除再新建同名 * 文件,將沒法繼續跟蹤文件 * -F 跟蹤文件名,至關於--follow=name --retry,當文件刪除再新建同名文件,將能夠繼續跟蹤文件
tail -f /var/log/messages ###跟蹤
tail -fn0 /var/log/messages ### 只看最新發生的日誌
ide
### cut * -d DELIMITER: 指明分隔符,默認tab * -f FILEDS: #: 第#個字段,例如:3 #,#[,#]:離散的多個字段,例如:1,3,6 #-#:連續的多個字段, 例如:1-6 混合使用:1-3,7 * -c 按字符切割 * --output-delimiter=STRING指定輸出分隔符
cut -d: -f1,3-5,7 /etc/passwd
[root@localhost ~]# echo {1..10} | cut -d' ' -f1,3,5-10
1 3 5 6 7 8 9 10
工具
[root@localhost ~]# ifconfig | head -n2 | tail -n1 | cut -d" " -f10
10.0.0.204
spa
[root@localhost ~]# ifconfig | head -n2 | tail -n1 | tr -s " " | cut -d " " -f3
10.0.0.204
[root@localhost ~]# df | tr -s " " | cut -d' ' -f5 | tr -dc "[0-9\n]"
00
2
0
2
1
14
0
df | tr -s ' ' % |cut -d% -f5 |tr -d '[:alpha:]'
df | cut -c44-46 |tr -d '[:alpha:]'
[root@centos8 ~]#cut -d: -f1,3,7 --output-delimiter="---" /etc/passwd
root---0---/bin/bash
bin---1---/sbin/nologin
daemon---2---/sbin/nologin
### paste paste 合併多個文件同行號的列到一行 * -d 分隔符:指定分隔符,默認用TAB * -s : 全部行合成一行顯示
paste -d":" alpha.log seq.log
paste -s seq.log
paste -s alpha.log seq.log
### wc #文本數據統計 * -l 只計數行數 * -w 只計數單詞總數 * -c 只計數字節總數 * -m 只計數字符總數 * -L 顯示文件中最長行的長度
[root@localhost ~]# wc a
6 7 49 a
[root@localhost ~]# wc -l a
6 a
[root@localhost ~]# cat a | wc -l
6
### sort #整理文本 * -r 執行反方向(由上至下)整理 * -R 隨機排序 * -n 執行按數字大小整理 * -f 選項忽略(fold)字符串中的字符大小寫 * -u 選項(獨特,unique),合併重複項,即去重 * -t c 選項使用c作爲字段界定符 * -k # 選項按照使用c字符分隔的 # 列來整理可以使用屢次
[root@localhost ~]# cut -d: -f1,3 /etc/passwd | sort -t: -k2 -nr | head -n3
def:1001
abc:1000
polkitd:999
[root@localhost ~]# cut -d" " -f1 /var/log/nginx/access_log |sort -u|wc -l
cut: /var/log/nginx/access_log: No such file or directory
0
[root@centos8 ~]#df| tr -s ' ' '%'|cut -d% -f5|sort -nr|head -1
100
### uniq 去重 uniq命令從輸入中刪除先後相接的重複的行 * -c: 顯示每行重複出現的次數 * -d: 僅顯示重複過的行 * -u: 僅顯示未曾重複的行
[root@localhost ~]# uniq a1 -c
2 aaaa
2 bbbb
1 ccc
[root@localhost ~]# uniq a1 -d
aaaa
bbbb
[root@localhost ~]# uniq a1 -u
ccc
[root@localhost ~]# sort a1 | uniq -c
2 aaaa
2 bbbb
1 ccc
2 ddd
[root@localhost ~]# ss -nt |tail -n+2 | tr -s ' '| cut -d" " -f4 | cut -d: -f1 | uniq -c
2 10.0.0.204
cut -d" " -f1 access_log |sort |uniq -c|sort -nr |head -3
lastb -f btmp-34 | tr -s ' ' |cut -d ' ' -f3|sort |uniq -c
|sort -nr | head -3
### diff和patch diffff 命令比較兩個文件之間的區別 ### patch 複製在其它文件中進行的改變**(要謹慎使用)** 適用 -b 選項來自動備份改變了的文件 ### cmp
[root@localhost ~]# cmp /usr/bin/dir /usr/bin/ls
/usr/bin/dir /usr/bin/ls differ: byte 645, line 1
# 練習 一、找出ifconfifig 「網卡名」 命令結果中本機的IPv4地址
[root@localhost ~]# ifconfig | head -n2 | tail -n1 | tr -s ' '| cut -d' ' -f3
10.0.0.204
二、查出分區空間使用率的最大百分比值
[root@localhost ~]# df | tr -s ' ' | tr -d % | cut -d' ' -f5 | tr -d '[:alpha:]' | sort -nr | head -n1
14
三、查出用戶UID最大值的用戶名、UID及shell類型
cut -d: -f3,1,7 /etc/passwd | sort -t: -nr -k2
四、查出/tmp的權限,以數字方式顯示
[root@localhost ~]# stat /tmp | head -n4 | tail -n1 | cut -d: -f2 | cut -d/ -f1 | cut -d'(' -f2
1777
五、統計當前鏈接本機的每一個遠程主機IP的鏈接數,並按從大到小排序
[root@localhost ~]# netstat -t | grep "ssh " | tr -s " " | cut -d' ' -f5 | cut -d: -f1 | uniq -c | sort -nr2 10.0.0.1