一 文件查看cat
- -E: 顯示行結束符$
創(chuàng)建一個文件——nano f1,在文本編輯中在b和d之間補一個tab鍵
[root@centos7 ~]#nano f1
[root@centos7 ~]#cat f1
a
d
b d
[root@centos7 ~]#nano f1
[root@centos7 ~]#cat -E f1
a$
b$
c d $
不執(zhí)行-E時吨悍,無法看
-n: 對顯示出的每一行進行編號
[root@centos7 ~]#cat -n f1
1 a
2 b
3 c d
-A:顯示所有控制符 ——將tab鍵位顯示出來
[root@centos7 ~]#cat -A f1
a$
b$
c^Id $
-b:非空行編號
[root@centos7 ~]#cat -n f1
1 a
2
3
4 b
5
6
7 c d
[root@centos7 ~]#cat -b f1
1 a
2 b
3 c d
-s:壓縮連續(xù)的空行成一行
image.pngtac——倒著顯示(行與行)
image.pngrev——每行字符倒著顯示
image.png
二 分頁查看文件內容
(1)more: 分頁查看文件
在看文件時,執(zhí)行cat /var/log/message時蹋嵌,不能看見每一頁內容育瓜,會直接顯示最后一行并退出
image.png
此時執(zhí)行more命令時,可以一頁一頁的顯示栽烂,按空格鍵往下翻頁躏仇,按b鍵往下翻頁,并顯示進度
image.png
more [OPTIONS...] FILE... -d: 顯示翻頁及退出提示 ?
less:一頁一頁地查看文件或STDIN輸出 ——作用和more類似腺办,pageup,pagedown上下翻頁
查看時有用的命令包括: /文本 搜索 文本 n/N 跳到下一個 或 上一個匹配
less 命令是man命令使用的分頁器
管道方式—— ls -R |more或是less來進行翻頁毕泌,但是more的管道方式不能往回翻頁
三 顯示文本前或后行內容
head(頭部)闽巩,顯示前多少個字節(jié)
不加選項的時候執(zhí)行head命令默認顯示前十行顯示:
image.png
-c #: 指定獲取前#字節(jié) ——head -c3 file 顯示該文件前三個字節(jié)的內容image.png
-n #: 指定獲取前#行——head -n3 顯示 /etc/passwd文件中的前三行內容
image.png
-#: 指定行數(shù)——直接 head -3,直接顯示前三行和-n效果一樣tail (尾部),顯示后多少個字節(jié)
不加選項時默認顯示后十行:
image.png
-c #: 指定獲取后#字節(jié)
image.png
-n #: 指定獲取后#行image.png
-#:原理同head
-f: 跟蹤顯示文件新追加的內容,常用日志監(jiān)控- 打開兩個終端觀察——tail -f mail.txt
[root@centos7 ~]#tail -f mail.txt
"help"
hi ,root
i am root
The system version is here,please help me to check it ,thanks!
CentOS release 6.9 (Final)
woaini
在另外一個終端窗口
[root@centos7 ~]#echo wo ai laowang >>mail.txt
在之前的窗口顯示
[root@centos7 ~]#tail -f mail.txt
"help"
hi ,root
i am root
The system version is here,please help me to check it ,thanks!
CentOS release 6.9 (Final)
woaini
wo ai laowang
四 按列抽取文本cut和合并文件paste
cut
- -d DELIMITER: 指明分隔符杆兵,默認tab
-f FILEDS: #: 第#個字段 #,#[,#]:離散的多個字段,例如1,3,6 #-#:連續(xù)的多個字段, 例如1-6 混合使用:1-3,5
[root@centos7 ~]#cut -d: -f1,3-5 /etc/passwd——以':'作為分隔符窿侈,取第一列和三到五列
root:0:0:root
bin:1:1:bin
daemon:2:2:daemon
adm:3:4:adm
lp:4:7:lp
sync:5:0:sync
shutdown:6:0:shutdown
-c 按字符切割
[root@centos7 ~]#who
root :0 2017-07-25 08:00 (:0)
root pts/2 2017-07-25 14:11 (:0)
root pts/3 2017-07-25 14:11 (192.168.136.1)
[root@centos7 ~]#who |cut -c-9
root
root
root
--output-delimiter=STRING指定輸出分隔符小練習
只顯示df內容中use利用率的那一列
方法一:[root@centos7 ~]#df |tr -s " "|cut -d" " -f5
Use%
10%
0%
1%
2%
0%
17%
1%
1%
100%
方法二:[root@centos7 ~]#df |tr -s " "|cut -d" " -f5|tr -d "%"
Use
10
0
1
2
0
17
1
1
100
方法三:[root@centos7 ~]#df |tr -s " "|cut -d" " -f5|cut -d% -f1
Use
10
0
1
2
0
17
1
1
100
方法四:[root@centos7 ~]#df |tr -s " " "%"|cut -d% -f5
Use
10
0
1
2
0
17
1
1
100
練習二 只顯示ifconfig ens33的ip地址
[root@centos7 ~]#ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.136.141 netmask 255.255.255.0 broadcast 192.168.136.255
inet6 fe80::28af:dcce:a76d:da1b prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:02:59:96 txqueuelen 1000 (Ethernet)
RX packets 12958 bytes 1090604 (1.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10349 bytes 7092395 (6.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@centos7 ~]#ifconfig ens33 |head -n2|tail -n1 |tr -s " "|cut -d" " -f3
192.168.136.141paste
意義:合并兩個文件同行號的列到一行
[root@centos7 ~]#cat f2
I LOVE YOU SISTER
\S
KERNEL \R ON AN \M
[root@centos7 ~]#cat f1
a
b
c d
[root@centos7 ~]#paste f1 f2
a I LOVE YOU SISTER
\S
b KERNEL \R ON AN \M
c d
-d 分隔符:指定分隔符念秧,默認用TAB
paste -d"---" f1 f2
[root@centos7 ~]#paste -d"---" f1 f2
a -I LOVE YOU SISTER
-
-\S
b -KERNEL \R ON AN \M
-
-
c d -
-s : 所有行合成一行顯示 ——paste -s f1 f2
a b c d
I LOVE YOU SISTER \S KERNEL \R ON AN \M
五 分析文本的工具
收集文本統(tǒng)計數(shù)據wc
計數(shù)單詞總數(shù)、行總數(shù)聊闯、字節(jié)總數(shù)和字符總數(shù) ?
可以對文件或STDIN中的數(shù)據運行 $
wc story.txt 39 237 1901 story.txt
行數(shù) 字數(shù) 字符數(shù) ?
使用 -l 來只計數(shù)行數(shù)——顯示文件一共有多少行使用
[root@centos7 ~]#cat f1
a
b
c d
[root@centos7 ~]#wc -l f1
7 f1
-w 來只計數(shù)單詞總數(shù) ?
[root@centos7 ~]#wc -w f1
4 f1
使用 -c 來只計數(shù)字節(jié)總數(shù) ?
[root@centos7 ~]#wc -c f1
31 f1——空格也算字節(jié)
使用 -m 來只計數(shù)字符總數(shù)
[root@centos7 ~]#cat /etc/issue
i love you sister
\S
Kernel \r on an \m
[root@centos7 ~]#wc -m /etc/issue
42 /etc/issue
文本排序sort
?把整理過的文本顯示在STDOUT工猜,不改變原始文件
[root@centos7 ~]#df |tr -s " " % |cut -d"%" -f5|sort
0
0
1
1
1
10
100
17
2
Use——按照每行第一字符大小排序
$ sort [options] file(s) ?
常用選項 ?
-n 執(zhí)行按數(shù)字大小整理
[root@centos7 ~]#df |tr -s " " % |cut -d"%" -f5|sort -n
0
0
Use
1
1
1
2
10
17
100
-r 執(zhí)行反方向(由上至下)整理 ?
sort -r /etc/issue
image.png
-f 選項忽略(fold)字符串中的字符大小寫 ?
-u 選項(獨特,unique)刪除輸出中的重復行
>* -t c 選項使用c做為字段界定符 ——類似cut -dimage.png
[root@centos7 ~]#sort -t: -k3 -nr /etc/passwd
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
tom:x:1009:1001::/app/jackdir:/bin/bash
libai:x:1008:1007::/home/libai:/bin/bash
likui:x:1007:1007::/home/likui:/bin/bash
sasles:x:1006:1007::/home/sasles:/bin/bash
sarah:x:1005:1006:sa,xiaoshou,10088,182568877:/home/sarah:/sbin/nologin
harry:x:1004:1005::/home/harry:/bin/bash
natasha:x:1003:1004::/home/natasha:/bin/bash
wangcai:x:1001:1001::/home/wangcai:/bin/bash
hangnan:x:1000:1000:zhangnan,jishubu,10086,1024:/home/zhangnan:/bin/bashuniq
uniq命令:從輸入中刪除前后相接的重復的行
image.png
uniq [OPTION]... [FILE]...
-c: 顯示每行重復出現(xiàn)的次數(shù)
image.png
image.png
-d: 僅顯示重復過的行
image.png
-u: 僅顯示不曾重復的行 連續(xù)且完全相同方為重復
image.png
常和sort 命令一起配合使用: sort userlist.txt | uniq -c
六 Linux文本處理三劍客 —grep
grep: Global search REgular expression and Print out the line
作用:文本搜索工具菱蔬,根據用戶指定的“模式”對目標文 本逐行進行匹配檢查篷帅;打印匹配到的行image.png
模式:由正則表達式字符及文本字符所編寫的過濾條件
image.png
--color=auto: 對匹配到的文本著色顯示image.png
-v: 顯示不被pattern匹配到的行image.png
-i: 忽略字符大小寫image.png
-c: 統(tǒng)計匹配的行數(shù)image.png
-n:顯示匹配的行號image.png
-o: 僅顯示匹配到的字符串
?image.png
-q: 靜默模式史侣,不輸出任何信息
image.png
-A #: after, 后#行image.png
-B #: before, 前#行 ?image.png
-C #:context, 前后各#行image.png
-e:實現(xiàn)多個選項間的邏輯or關系 grep –e ‘cat ’ -e ‘dog’ fileimage.png
-w:匹配整個單詞 :單詞的含義是'wang' 'wang-laozhang' 'wang:zhang' 而'wang_lao'不是單詞?
-E:使用ERE ——等于egerp?
-F:相當于fgrep,不支持正則表達式基本正則表達式元字符
字符匹配: . 匹配任意單個字符 'r..t'匹配兩個字符
image.png
[] 匹配指定范圍內的任意單個字符——grep [root] /etc/passwd
image.png
[^] 匹配指定范圍外的任意單個字符—— grep [^root] /etc/passwd
image.png
[:alnum:] 字母和數(shù)字
[:alpha:] 代表任何英文大小寫字符魏身,亦即 A-Z, a-z
[:lower:] 小寫字母 [:upper:] 大寫字母
[:blank:] 空白字符(空格和制表符)
[:space:] 水平和垂直的空白字符(比[:blank:]包含的范圍廣)
[:cntrl:] 不可打印的控制字符(退格惊橱、刪除、警鈴...)
[:digit:] 十進制數(shù)字
[:xdigit:]十六進制數(shù)字
[:graph:] 可打印的非空白字符
[:print:] 可打印字符
[:punct:] 標點符號正則表達式
匹配次數(shù):用在要指定次數(shù)的字符后面叠骑,用于指定前面的字 符要出現(xiàn)的次數(shù)
'星號' 匹配前面的字符任意次李皇,包括0次 貪婪模式:盡可能長的匹配
.* 任意長度的任意字符
? 匹配其前面的字符0或1次
+ 匹配其前面的字符至少1次
{n} 匹配前面的字符n次
{m,n} 匹配前面的字符至少m次,至多n次
{,n} 匹配前面的字符至多n次
{n,} 匹配前面的字符至少n次
位置錨定:定位出現(xiàn)的位置
^ 行首錨定宙枷,用于模式的最左側
$ 行尾錨定掉房,用于模式的最右側
^PATTERN$ 用于模式匹配整行
^$ 空行 ^[[:space:]]$ 空白行
< 或 \b 詞首錨定,用于單詞模式的左側
> 或 \b 詞尾錨定慰丛;用于單詞模式的右側
<PATTERN> 匹配整個單詞
分組:() 將一個或多個字符捆綁在一起卓囚,當作一個整體進 行處理,如:(root)+ ? 分組括號中的模式匹配到的內容會被正則表達式引擎記錄于內部的變量中诅病,這些變量的命名方式為: \1, \2, \3, ... ?
\1 表示從左側起第一個左括號以及與之匹配右括號之間的 模式所匹配到的字符 ?
示例: (string1+(string2)) \1 :string1+(string2)* \2 :string2 ?
后向引用:引用前面的分組括號中的模式所匹配字符哪亿,而非 模式本身 ?
或者:|
示例:a|b: a或b C|cat: C或cat (C|c)at:Cat或cat'以練習題來展示上述命令'
1、顯示/proc/meminfo文件中以大小s開頭的行(要求:使用兩 種方法)
image.png
2贤笆、顯示/etc/passwd文件中不以/bin/bash結尾的行 ?
image.png
3蝇棉、顯示用戶rpc默認的shell程序 ?
image.png
4、找出/etc/passwd中的兩位或三位數(shù)
image.png
?
5芥永、顯示CentOS7的/etc/grub2.cfg文件中篡殷,至少以一個空白 字符開頭的且后面存非空白字符的行 ?
image.png
6、找出“netstat -tan”命令的結果中以‘LISTEN’后跟任意多 個空白字符結尾的行
image.png
7埋涧、顯示CentOS7上所有系統(tǒng)用戶的用戶名和UID
image.png
8板辽、添加用戶bash、testbash棘催、basher劲弦、sh、nologin(其shell 為/sbin/nologin),找出/etc/passwd用戶名同shell名的行 ?
image.png
9醇坝、利用df和grep邑跪,取出磁盤各分區(qū)利用率,并從大到小排序
image.png
egrep及擴展的正則表達式
egrep = grep -E ?egrep [OPTIONS] PATTERN [FILE...] ?
擴展正則表達式的元字符: ?
字符匹配:
. 任意單個字符 [] 指定范圍的字符 [^] 不在指定范圍的字符
次數(shù)匹配:
*:匹配前面字符任意次
?: 0或1次
+:1次或多次
{m}:匹配m次
{m,n}:至少m呼猪,至多n次
位置錨定: ^ :行首 $ :行尾 <, \b :語首 >, \b :語尾 ?
分組: () 后向引用:\1, \2, ... ?
或者: a|b: a或b C|cat: C或cat (C|c)at:Cat或cat以練習題來解釋上述命令
1呀袱、顯示三個用戶root、mage郑叠、wang的UID和默認shell
image.png
2夜赵、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包 括下劃線)后面跟一個小括號的行 ?
image.png
3、使用egrep取出/etc/rc.d/init.d/functions中其基名 ?
image.png
4乡革、使用egrep取出上面路徑的目錄名
image.png
5寇僧、統(tǒng)計last命令中以root登錄的每個主機IP地址登錄次數(shù)
image.png