1 檔案内容檢視指令
1.1 檢視文本檔案内容
1.1.1 cat
cat 可以檢視文本内容
格式:
cat [選項]... 檔案
常見選項:
-E:顯示行結束符$
-A:顯示所有控制符
-n:對顯示出的每一行進行編号
-b:非空行編号
-s:壓縮連續的空行成一行
案例:
[root@nginx ~]# cat da.txt
a b
c
d b c
[root@nginx ~]# cat -A da.txt
a b$
c $
d b c $
[root@nginx ~]#
[root@nginx ~]# cat db.txt
a
b
c
[root@nginx ~]# hexdump -C db.txt
00000000 61 20 20 0a 62 20 20 0a 63 20 20 0a |a .b .c .|
0000000c
[root@nginx ~]#
[root@nginx ~]# cat -A db.txt
a $
b $
c $
[root@nginx ~]# file db.txt
db.txt: FORTRAN program, ASCII text
[root@nginx ~]#
1.1.2 nl
顯示行号,相當于cat -b
案例:
[root@nginx ~]# cat db.txt
a
b
c
[root@nginx ~]# nl db.txt
1 a
2 b
3 c
[root@nginx ~]#
1.1.3 tac
逆向顯示文本内容
案例:
[root@nginx ~]# cat db.txt
a
b
c
[root@nginx ~]# tac db.txt
c
b
a
[root@nginx ~]#
[root@nginx ~]# tac
a
b
c #這裡按回車後,在按ctrl+d
c
b
a
[root@nginx ~]# seq 5 | tac
5
4
3
2
1
[root@nginx ~]#
1.1.4 rev
将同一行的内容逆向顯示
案例:
[root@nginx ~]#cat ta.txt
1 2 3 4 5
a b c
[root@nginx ~]#tac ta.txt
a b c
1 2 3 4 5
[root@nginx ~]#rev
abcdef
fedcba
[root@nginx ~]#echo {1..10} |rev
01 9 8 7 6 5 4 3 2 1
1.2 檢視非文本檔案内容
1.2.1 hexdump
案例:
[root@nginx ~]# hexdump -C -n 512 /dev/sda
00000000 eb 63 90 10 8e d0 bc 00 b0 b8 00 00 8e d8 8e c0 |.c..............|
00000010 fb be 00 7c bf 00 06 b9 00 02 f3 a4 ea 21 06 00 |...|.........!..|
[root@nginx ~]# echo {a..z} | tr -d ' '|hexdump -C
00000000 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 |abcdefghijklmnop|
00000010 71 72 73 74 75 76 77 78 79 7a 0a |qrstuvwxyz.|
0000001b
[root@nginx ~]#
1.2.2 xxd
[root@nginx ~]# echo {a..z} | tr -d ' '|xxd
0000000: 6162 6364 6566 6768 696a 6b6c 6d6e 6f70 abcdefghijklmnop
0000010: 7172 7374 7576 7778 797a 0a qrstuvwxyz.
1.3 分頁檢視檔案内容
1.3.1 more
可以實作分頁檢視檔案,可以配合管道實作輸出資訊的分頁
格式:
more [選項] 檔案
選項:
- -d: 顯示翻頁及退出提示
案例:
[root@nginx log]# more /var/log/messages
[Enter]:向下翻一行
[Space]:向下翻一頁
按f往下翻,按b往回翻
q:退出
1.3.2 less
可以實作分頁檢視檔案或STDIN輸出,less 指令是man指令使用的分頁器
檢視時常用的指令:
#以下是末行模式下使用
/接着輸入想查找的文本内容
##查找搜尋後的結果
n:表示跳到下一個
N:表示比對上一個
案例:
[root@nginx log]# cat /var/log/messages |less
Jun 25 14:01:01 nginx systemd: Started Session 3 of user root.
Jun 25 14:01:01 nginx systemd: Starting Session 3 of user root.
Jun 25 15:01:01 nginx systemd: Started Session 4 of user root.
Jun 25 15:01:01 nginx systemd: Starting Session 4 of user root.
....省略不展開
顯示文本前或後行内容
1.3.3 head
可以顯示檔案或标準輸入的前面行
格式:
head [選項]... 檔案
選項:
-c:指定擷取前幾位元組
-n:指定擷取前幾行
案例:
[root@nginx ~]# head /etc/group //預設檢視檔案的前10行
root:x:0:
bin:x:1:
daemon:x:2:
...省略
[root@nginx ~]# head -n 3 /etc/group //隻檢視檔案的前3行
[root@nginx ~]# echo a和b | head -c4
a和[root@nginx ~]#
1.3.4tail
tail 和head 相反,檢視檔案或标準輸入的倒數行
格式:
tail [選項]... 檔案
常用選項:
-c:指定擷取後#位元組
-n:指定擷取後#行
-f:跟蹤顯示檔案新追加的内容,常用日志監控,相當于 --follow=descriptor,當檔案删除再新
建同名檔案,将無法繼續跟蹤檔案
-F:跟蹤檔案名,相當于--follow=name --retry,當檔案删除再建立同名檔案,将可以繼續跟蹤文
件
tailf:類似 tail –f,當檔案不增長時并不通路檔案
案例:
[root@nginx ~]# tail -3 /var/log/messages
Jun 27 18:01:01 nginx systemd: Starting Session 4 of user root.
Jun 27 19:01:02 nginx systemd: Started Session 5 of user root.
Jun 27 19:01:02 nginx systemd: Starting Session 5 of user root.
[root@nginx ~]#
#顯示最後兩行的觀察,不想看了就直接ctrl+c中斷
[root@nginx ~]#tail -2f /var/log/messages
[root@nginx ~]# ifconfig | head -2 | tail -1
inet 192.168.223.111 netmask 255.255.255.0 broadcast 192.168.223.255
[root@nginx ~]#
1.4 按列抽取文本cut
cut 指令可以提取文本檔案或STDIN資料的指定列
格式:
cut [選項]... 檔案
常用選項:
-d:指明分隔符,預設tab
-f:FILEDS:
#: 第#個字段,例如:3
#,#[,#]:離散的多個字段,例如:1,3,6
#-#:連續的多個字段, 例如:1-6
混合使用:1-3,7
-c:按字元切割
--output-delimiter=STRING指定輸出分隔符
案例:
[root@nginx ~]# ifconfig |head -n2 |tail -n1|cut -d" " -f10
192.168.223.111
[root@nginx ~]# ifconfig |head -n2 |tail -n1|tr -s " " |cut -d " " -f3
192.168.223.111
[root@nginx ~]#
[root@nginx ~]# df | cut -c44-46 |tr -d '[:alpha:]'
% 挂
694
994
006
996
006
273
201
[root@nginx ~]#
[root@nginx ~]# df|tr -s ' ' '%'|cut -d% -f5
已用
3
0
0
1
0
33
0
[root@nginx ~]#
1.5合并多個檔案 paste
paste 合并多個檔案同行号的列到一行
格式:
paste [選項]... 檔案
常用選項:
-d:分隔符,指定分隔符,預設用tab
-s:所有行合成一行顯示
案例:
[root@nginx ~]# cat b.txt
1
2
3
[root@nginx ~]# cat q.txt
a
b
c
[root@nginx ~]# cat q.txt b.txt
a
b
c
1
2
3
[root@nginx ~]# paste q.txt b.txt
a 1
b 2
c 3
[root@nginx ~]# paste -d":" q.txt b.txt
a:1
b:2
c:3
[root@nginx ~]# paste -s q.txt
a b c d
[root@nginx ~]#
[root@nginx ~]# paste -s q.txt b.txt
a b c d
1 2 3 4
[root@nginx ~]#
1.6分析文本的工具
- 文本資料統計:wc
- 整理文本:sort
- 比較檔案:diff和patch
1.6.1 收集文本統計資料wc
wc 指令可用于統計檔案的行總數、單詞總數、位元組總數和字元總數可以對檔案或STDIN中的資料統計
常用選項:
-l:隻計數行數
-w:隻計數單詞總數
-c:隻計數位元組總數
-m:隻計數字元總數
-L:顯示檔案中最長行的長度
案例:
[root@nginx ~]# cat q.txt
a
b
c
d
[root@nginx ~]# wc -l q.txt
4 q.txt
[root@nginx ~]# cat q.txt |wc -l
4
[root@nginx ~]# wc q.txt
4 4 8 q.txt
1.6.2 文本排序 sort
把整理過的文本顯示在STDOUT,不改變原始檔案
格式:
sort [選項] 檔案
常用選項:
-r:執行反方向(由上至下)整理
-R:随機排序
-n:執行按數字大小整理
-h:人性化可讀排序,如: 2K 1G
-f:選項忽略(fold)字元串中的字元大小寫
-u:選項(獨特,unique),合并重複項,即去重
-t:c 選項使用c做為字段界定符
-k:#選項按照使用c字元分隔的 # 列來整理能夠使用多次
案例:
[root@nginx ~]# cut -d: -f1,3 /etc/passwd|sort -t: -k2 -nr |head -n3
zhangsan:1003
lisi:1002
wangwu:1001
#檢視分區使用率最高值
[root@nginx ~]# df| tr -s ' ' '%'|cut -d% -f5|sort -nr|head -1
33
[root@nginx ~]# df | tr -s " " %|cut -d% -f5|tr -d '[:alpha:]' | sort
0
0
0
0
1
3
33
已用
[root@nginx ~]#
1.6.3 去重uniq
uniq指令從輸入中删除前後相接的重複的行
格式:
uniq [選項] 檔案
常見選項:
-c:顯示每行重複出現的次數
-d:僅顯示重複過的行
-u:僅顯示不曾重複的行
uniq常和sort 指令一起配合使用:
案例:
[root@nginx ~]# cat q.txt
a
b
c
d
a
b
d
f
g
h
j
#顯示q.txt檔案重複的字母次數
[root@nginx ~]# sort q.txt |uniq -c
2 a
2 b
1 c
2 d
1 f
1 g
1 h
1 j
[root@nginx ~]#
[root@nginx ~]#cat te1.txt
a
b
1
c
[root@nginx ~]#cat te2.txt
b
e
f
c
1
2
#取檔案的共同行
[root@nginx ~]#cat te1.txt te2.txt | sort |uniq -d
1
b
c
#取檔案的不同行
[root@nginx ~]#cat te1.txt te2.txt | sort |uniq -u
2
a
e
f
工作中基本會用到文本處理工具都在這裡了,感興趣的可以看看。。。