在shell中使用最多的支持正則表達式的處理文本的三個命令:
linux
①grep:默認不支持擴展表達式,加-E選項開啓ERE(擴展正則表達式),若是不加-E使用花括號要加轉義符\{\}--------egrep支持基礎和擴展正則表達式正則表達式
②awk:支持egrep全部的正則表達式shell
③sed:默認不支持擴展表達式,加-r選項開啓ERE。若是不加-r使用花括號要加轉義符\{\}編程
grepvim
格式:grep 參數 文件數組
做用:用於在文本中執行關鍵詞搜索,並顯示匹配結果緩存
參數:app
-E 模式是擴展正則表達式less
-i 忽略大小寫編程語言
-n 打印行號
-o 只打印匹配內容
-c 只顯示匹配次數
-B 打印匹配的前幾行
-A 打印匹配的後幾行
-C 打印匹配的先後幾行
--color 匹配字體顏色
-v 取反,打印不匹配的行
awk
格式:
做用:awk不只僅是linux系統中的一個命令,並且是一種編程語言,用來處理數據,生成報告,能夠在命令行直接操做,也能夠編寫成awk程序來進行更復雜的運用
參數:
-F:指定分隔符
NR:記錄的編號,awk每讀取一行,NR就+1,能夠理解爲行號
NF:字段數量,記錄了當前行包含多少字段,能夠理解爲有多少列
FS:字符分隔符,默認的以空格爲分隔符
輸出字段表達方式
$1 $2 $3............$n 輸出一個指定的字段
$NF 輸出最後一個字段
$0 輸出整條記錄
awk執行過程
awk進階--正則
練習:提取本機IP地址
awk特殊模式-BEGIN模式與END模式
BEGIN模塊在awk讀取文件以前執行,通常來定義內置變量(預約義變量,eg:FS,RS)
須要注意的是BEGIN模塊後面要接一個action操做塊,包含在大括號內。
BEGIN模塊
END模塊
EHD在awk讀取完全部的文件的時候,再執行END模塊,通常用來輸出一個結果(累加,數組結果),也能夠是和BEGIN模塊相似的結尾標識信息
與BEGIN模式相對應的END模式,格式同樣,可是END模式僅在awk處理完全部輸入行後才進行處理。
sed
格式:sed 參數 命令 文件
說明:
1,注意sed軟件以及後面選項,sed命令和輸入文件,每一個元素之間都至少有一個空格。
2,sed -commands(sed命令)是sed軟件內置的一些命令選項,爲了和前面的options(選項)區分,故稱爲sed命令
3,sed -commands 既能夠是單個sed命令,也能夠是多個sed命令組合。
4,input -file (輸入文件)是可選項,sed還可以從標準輸入如管道獲取輸入。
做用:編輯文件
工做原理:
sed讀取一行,首先將這行放入到緩存中
而後,纔對這行進行處理
處理完成之後,將緩衝區的內容發送到終端
存儲sed讀取到的內容的緩存區空間稱之爲:模式空間(Pattern Space)
參數:
參數 | 解釋說明 |
-n no |
取消默認的軟件的輸出,常與sed的命令的p連用 |
-e entry | 一行命令語句能夠執行多條sed命令,多分支 |
-r ruguler | 使用擴展正則表達式,默認狀況sed只識別基本正則表達式 |
-i inside | 直接修改文件內容,而不是輸出到終端 |
命令 | 解釋說明 |
a append | 追加,在行後添加一行或多行文本 |
c change | 取代指定的行 |
d delete | 刪除指定的行 |
i insert | 插入,在指定行前添加一行或多行文本 |
p print | 答應模式空間內容,常與-n一塊兒連用 |
特殊符號 | 解釋說明 |
! | 對指定行之外的全部行執行命令 |
這裏咱們須要用到2個sed命令,分別是:
實例1:a
[root@ken ~]# sed "2a 這是新添加的一行" test this is the first line this is the second line 這是新添加的一行 this is the third line this is the forth line this is the fivth line this is the sixth line this is the seventh line this is the eighth line this is the ninth line this is the tenth line
實例2:i
[root@ken ~]# sed "2i 我又新添加了一行" test this is the first line 我又新添加了一行 this is the second line this is the third line this is the forth line this is the fivth line this is the sixth line this is the seventh line this is the eighth line this is the ninth line this is the tenth line
實例3:同時增長多行(/n)
[root@ken ~]# sed "2i 這是第一條記錄\n這是第二條記錄\n這是第三條記錄" test this is the first line 這是第一條記錄 這是第二條記錄 這是第三條記錄 this is the second line this is the third line this is the forth line this is the fivth line this is the sixth line this is the seventh line this is the eighth line this is the ninth line this is the tenth line
實例1:刪除全部的行
[root@ken ~]# cp test{,.bak} [root@ken ~]# sed 'd' test
命令說明:若是在sed命令前面不指定地址範圍,那麼默認會匹配全部行,而後使用d命令刪除功能就會刪除這個文件的全部內容
實例2:刪除指定的行
[root@ken ~]# cat test.bak >test [root@ken ~]# sed '2d' test this is the first line this is the third line this is the forth line this is the fivth line this is the sixth line this is the seventh line this is the eighth line this is the ninth line this is the tenth line
實例3:刪除指定範圍行
[root@ken ~]# sed '2,5d' test this is the first line this is the sixth line this is the seventh line this is the eighth line this is the ninth line this is the tenth line
實例4:刪除匹配的行
[root@ken ~]# sed '/sixth/d' test this is the first line this is the second line this is the third line this is the forth line this is the fivth line this is the seventh line this is the eighth line this is the ninth line this is the tenth line
命令說明:在sed軟件中,使用正則的格式和awk同樣,使用2個」/「包含指定的正則表達式,即「/正則表達式/」。
實例5:刪除指定行到行尾的內容
[root@ken ~]# sed '2,$d' test this is the first line
第二行也會被刪掉
實例6:取反
1、
[root@ken ~]# sed '2,3!d' test this is the second line this is the third line
2、
[root@ken ~]# sed '/tenth/!d' test this is the tenth line
[root@ken ~]# sed '2c 改過以後的第二行' test this is the first line 改過以後的第二行 this is the third line this is the forth line this is the fivth line this is the sixth line this is the seventh line this is the eighth line this is the ninth line this is the tenth line this is sixth line
sed -i 's/目標內容/替換內容/g' ken.log
sed -i 's#目標內容#替換內容#g'
實例1:
[root@ken ~]# sed 's/line/hang/g' test this is the first hang this is the second hang this is the third hang this is the forth hang this is the fivth hang this is the sixth hang this is the seventh hang this is the eighth hang this is the ninth hang this is the tenth hang this is sixth hang
命令說明:從上面命令的結果咱們就知道sed命令默認不會修改文件的內容
實例2:
[root@ken ~]# sed -i 's/line/hang/g' test [root@ken ~]# cat test this is the first hang this is the second hang this is the third hang this is the forth hang this is the fivth hang this is the sixth hang this is the seventh hang this is the eighth hang this is the ninth hang this is the tenth hang this is sixth hang
命令說明:若是想真正的修改文件內容,咱們就須要使用選項「-i」,這個要和sed命令「i」區分開來。同時咱們能夠發現命令執行後的結果是沒有任何輸出的。
實例1:
[root@ken ~]# sed '2p' test this is the first hang this is the second hang this is the second hang this is the third hang this is the forth hang this is the fivth hang this is the sixth hang this is the seventh hang this is the eighth hang this is the ninth hang this is the tenth hang this is sixth hang [root@ken ~]# sed -n '2p' test this is the second hang
實例2:
[root@ken ~]# sed -n '2,5p' test this is the second hang this is the third hang this is the forth hang this is the fivth hang
實例3:
[root@ken ~]# sed -n '/ninth/p' test this is the ninth hang
實例1:
[root@ken ~]# sed -e '2d' -e '5d' test this is the first hang this is the third hang this is the forth hang this is the sixth hang this is the seventh hang this is the eighth hang this is the ninth hang this is the tenth hang this is sixth hang
實例2:
[root@ken ~]# sed -n -e '2p' -e '5p' test this is the second hang this is the fivth hang
1.查找指定的字符串
例子:顯示/etc/passwd中保含root的行(顯示模式空間中的內容)
方法1:set '/root/p' /etc/passwd 方法2:cat /etc/passwd | sed '/root/p'
2.在指定的位置作增刪
例子:刪除以root爲開頭的行
# sed '/^root/d' a.txt
例子:在包含root的行後添加一行 i am ken
# sed '/root/a i am ken' a.txt
3.按行替換
例子:將5到9行的內容替換爲 i am ken
# sed '5,9c i am ken' a.txt
4.按照字符替換
例子:將/etc/selinux/config中的SELINUX=enforcing改爲 disabled
寫法1:# sed -i 's/SELINUX=disabled/SELINUX=enforcing/g' config
寫法2:# sed -r -i 's/(SELINUX=)disabled/\1enforcing/g' config
5.查找指定的內容再作替換
例子:將以r開頭的行中的oo替換爲qq
# sed '/^r/{s/oo/qq/g}' passwd
6.多點編輯
例子:去除文件中的註釋行和空白行
# grep -v -E "(^#)|(^$)" passwd.bak >passwd
# cat passwd.bak | sed -e '/^#/d' -e '/^$/d' >passwd
7)取反操做
顯示非1-3行
# sed -n '1,3!p' passwd