接上篇,sed替換命令有四種替換標記mysql
在第一種替換標記中,使用數字,能夠指定替換文本行中字符出現的準確位置,例子以下:linux
- $ sed 's/test/study/2' test2.txt
- this is script test, we are study bash shell
- this is script test, we are study bash shell
- this is script test, we are study bash shell
- this is script test, we are study bash shell
- this is script test, we are study bash shell
此例子,是直接指定替換每行裏面第二次出現的匹配字符,第一個匹配字符是不被替換的。這就是數字替換標記的妙用。sql
g替換標記可以替換文本模式中每個出現的匹配字符。shell
- $ sed 's/test/study/g' test2.txt
- this is script study, we are study bash shell
- this is script study, we are study bash shell
- this is script study, we are study bash shell
- this is script study, we are study bash shell
- this is script study, we are study bash shell
與上述數字替換標記相比,g將全部出現的匹配字符所有替換爲須要的新字符了。bash
p替換標記會打印包含替換命令中匹配模式的那一行,常常和-n選項一塊兒使用。編輯器
示例以下:ide
- $ cat test2.txt
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- $ sed -n 's/test/study/p' test2.txt
- this is script study, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
只有加-p的時候,才能打印匹配行,不加-p,執行命令後,不會有任何顯示。post
w替換標記生成相同的輸出,可是會將輸出保存到制定的文件中:this
- $ sed 's/test/study/w test3' test2.txt
- this is script study, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
- $ cat test3
- this is script study, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
看到了吧,加上w參數後,直接將替換輸出的文本內容保存到新建的test3文本中了,很實用的替換標記。須要注意的是,w參數只將被替換的文本行輸出到要保存的文本當中。spa
在文本字符串當中,咱們還會遇到不容易在替換模式中實用的字符,在linux環境下最多見的就是正斜槓了。替換某個文件中的路徑名,會以爲很困難。舉個例子,要用cshell替換/etc/passwd文件中的bash shell,必須按照下面這樣作:
- $ sed 's/\/bin\/bash/\/bin\/csh/' /etc/passwd/
正斜槓被用做字符串定界符,若正斜槓出如今模式文本中,必須使用反斜槓使其轉義,不然會致使錯誤和混淆。
要解決這個問題,也有一個辦法,sed編輯器容許替換命令中的字符串定界符選擇一個不一樣的字符:
- $ sed 's!/bin/bash!/bin/csh!' /etc/passwd
默認狀況下sed編輯器使用的命令應用於文本中全部的數據行,若是咱們只想改變文本但中的某一行或者一組數據行的話,那就要使用到行尋址。在sed編輯器當中,有兩種行尋址的形式。
兩種形式使用相同格式的指定地址:
- [address]command
同時,也能將多個命令組合在一塊兒,應用到特定地址上:
- address {
- command1
- command2
- command3
- }
sed編輯器將指定的每個命令僅用於指定地址匹配的行
第一種:數字尋址法
所謂數字尋址法,就是利用數字表示行號,利用sed編輯器能夠修改掉指定的數字行號。在數字尋址當中,sed編輯器默認將文本中的第一行定義爲數字1,接下來的行號延續下去。在命令中指定的地址能夠是單個行號,也能夠由起始行號、逗號和結束行號來指定一個尋址範圍。看下面這個例子:
- $ sed '2s/study/test/' test2.txt
- this is script study, we are test bash shell
- this is script test, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
這個例子很明顯,咱們將文本中第二行利用數字2 尋址,將study替換爲test了。
繼續看下面這個例子,咱們使用文本行數字範圍進行尋址替換:
- $ sed '2,3s/study/test/' test2.txt
- this is script study, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script study, we are test bash shell
- this is script study, we are test bash shell
指定數字範圍後,將指定的2,3兩行的字符串替換爲須要的字符串了,真的很方便。
若是要將命令應用於文本行內某一點開始直到文本結束的一組文本行,那就須要用到特殊符號—美圓符號$
- $ sed '2,$s/study/test/' test2.txt
- this is script study, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
$在這裏發揮了最佳的數字尋址做用!
第二種:使用文本模式篩選器
文本模式篩選器稍微有些複雜,命令格式爲:
- /pattern/command
在使用文本篩選器的過程當中,必需要使用正斜槓標記指定的pattern。sed編輯器只將該命令應用於指定的文本模式行。看下面這個例子,若是咱們只想更改用戶renyudi2默認的shell,可使用以下方式:
- $ sed '/renyudi2/s/bash/csh/' /etc/passwd
- renyudi2:x:501:501::/home/renyudi2:/bin/csh
- mysql:x:502:502::/home/mysql:/bin/bash
- extmail:x:503:503::/home/extmail:/bin/bash
- postfix:x:504:504::/home/postfix:/bin/bash
- vmta:x:5000:5000::/var/vmta:/bin/bash
- sysadmin:x:5001:5001::/home/sysadmin:/bin/bash
sed編輯器有時候在處理文本字符多處的時候,就要用到組合命令了。組合命令須要將所用要執行的命令用大括號擴起來。這個時候,sed編輯器將處理地址行上列出的全部命令。實例以下:
- $ sed '2{
- > s/script/study/
- > s/test/learn/g
- > }' test2.txt
- this is script test, we are test bash shell
- this is study learn, we are learn bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
多個命令組合的效果至關不錯哦,上述例子指定替換了第二行的兩個字符,並且在替換/test/learn/的時候還加了g,替換了行中出現的所有匹配字符。組合命令超強吧!本節就先記錄到此,下篇繼續!