
Linux最最最最重要的哲學思想就是:一切皆檔案。檔案以及檔案的操作在LInux作業系統中是非常的重要。熟練使用精悍小巧快捷的文本處理方式讓效率更高。
一、檔案檢視指令
less 分頁檢視
[root@centos7 app]# cat /etc/passwd |less
cat
空行指的是沒有任何字元,如果一行中隻有空格那也不能稱為空行。
- 對每一行進行編号,不論一行有沒有内容都顯示行号。
[root@centos7 app]# cat -n /etc/passwd
-
對非空行進行編号
如果一行當中隻有空格,那麼這一行也不是非空行。使用
時也進行編号-b
[root@centos7 app]# cat -b file
-
壓縮空行
壓縮連續的空行為一行,一行中隻有空格那也不是空行
[root@centos7 app]# cat -s file
- 在每一行結束地添加$
[root@centos7 app]# cat -E $ /etc/passwd
二、分頁檢視内容
-
分頁檢視more
能顯示檔案剩餘的百分比
[root@centos7 app]# more passwd [root@centos7 app]# cat passwd | more
- 分頁檢視less
less可以使用搜尋使用[root@centos7 app]# cat /etc/profile.d/colorls.sh | less
搜尋字元串。/string
或者n
N
跳轉到下一個或者上一個比對。
***
三、截取文本的前行或者後行
-
head
截取指定前幾行或者前幾位元組的内容
[root@centos7 app]# cat colorls.sh | head -c 10 #顯示指定前10位元組 [root@centos7 app]# cat colorls.sh | head -n 10 #顯示指定前10行 [root@centos7 app]# cat colorls.sh | head -10
-
tail
tail與head正好相反,作用是截取文本的後幾行或後幾位元組。
tail與head常常搭配使用截取除指定的行
[root@centos7 app]# cat clorls.sh | tail -c 10 #顯示指定後10位元組 [root@centos7 app]# cat clorls.sh | tail -n 10 #顯示指定後10行 [root@centos7 app]# cat clorls.sh | tail -10 #顯示指定後10行
- head與tail搭配使用,精确找出第三行
[root@centos7 app]# cat file|head -3|tail -1
四、截取文本的某一列
-
cut
cut可以指定分隔符後,截取出指定的列。如果一行沒有分隔符的話,整行輸出。
#指定冒号為分隔符,隻顯示第2列
[root@centos7 app]# cat file |cut -d: -f2
#指定冒号為分隔符,顯示2到3列
[root@centos7 app]# cat file |cut -d: -c2-3
#
#如果分隔符有歧義,那麼需要加引号
[root@centos7 app]# cat file |cut -d";" -f2
-
paste
将兩個文本同行号的内容,輸出到一行
預設使用tab做分隔符
[root@centos7 app]# paste file file2
1 a
1 a
1 a
#指定冒号作文分隔符
[root@centos7 app]# paste -d: file file2
#将所有内容輸出到一行
[root@centos7 app]# paste -s file file2
1 1 1
a a a
五、文本統計工具與排序
- wc可以用來統計行數,字數,字元數
[root@centos7 app]# wc passwd 42 88 2308 passwd 行數 文字數 字元數 檔案名 # #統計行數 [root@centos7 app]# wc passwd -l 42 passwd #統計字元數 [root@centos7 app]# wc passwd -w 88 passwd #統計位元組數 [root@centos7 app]# wc passwd -c 2308 passwd
- sort工具用來對文本進行排序
#删除重複的行 [root@centos7 app]# sort -u file2 #按字數的大小進行排序 [root@centos7 app]# sort -n file2 # -r 執行反方向(由上至下)整理 # -n 執行按數字大小整理 # -f 選項忽略(fold)字元串中的字元大小寫 # -u 選項(獨特,unique)删除輸出中的重複行 # -t c 選項使用c做為字段界定符 # -k X 選項按照使用c字元分隔的X列來整理能夠使用多次
- uniq 删除連續重複的行為一行
壓縮連續重複的行為一行 [root@centos7 app]# uniq file2 #統計每行重複出現的次數 [root@centos7 app]# uniq file2 -c #顯示不曾連續重複的行 [root@centos7 app]# uniq file2 -u
練習題
1、找出ifconfig “網卡名” 指令結果中本機的IPv4位址
[root@centos7 app]# ifconfig ens33 |head -2|tail -1|tr -s ' '|cut -d ' ' -f3
192.168.0.129
解析
ifconfig ens3--------顯示網卡資訊
head -2|tail -1------保留IP資訊的一行
tr -s ' ' -----------将空格壓縮
cut -d ' ' -f3-------空格為分隔符顯示出第三列
2、查出分區空間使用率的最大百分比值
[root@centos7 app]# df -h| tr -s ' '|cut -d ' ' -f5
Use%
7%
0%
1%
tr -s ' '--------------壓縮空格
cut -d ' ' -f5---------以空格為分隔符,截取第五列
3、查出使用者UID最大值的使用者名、UID及shell類型
[root@centos7 app]# cut -d: -f1,3,7 /etc/passwd|sort -t : -k 2 -n|tail -1
nfsnobody:65534:/sbin/nologin
cut -d: -f1,3 /etc/passwd-----以:為分隔符,取1,3列
sort -t : -k 2 -n-------------以:作為分隔符,按第二列從小到大排序
tail -1-----------------------排序後最後一行是最大的,是以取最後一行
4、查出/tmp的權限,以數字方式顯示
#用指令本身的快捷方法
[root@centos7 app]# stat -c %a /tmp/
1777
#用本章的知識解決
[root@centos7 app]# stat /tmp/ | head -n4|tail -1|tr '(' '/'|cut -d/ -f2
1777
stat /tmp/ -----------------此指令可以檢視到目錄數字形式的權限
head -n4--------------------數字權限在第四行,是以取出前四行
tail -1---------------------取出隻有數字權限的一行
|tr '(' '/'-----------------設定分隔符
cut -d/ -f2-----------------取出權限