sed命令 | 做用 |
a | 在匹配行後面加入文本 |
c | 字符轉換 |
d | 刪除行 |
D | 刪除第一行 |
i | 在匹配行前面接入文本 |
hjavascript |
複製模板塊的內容到存儲空間 |
H | 追加模板塊的內容到存儲空間 |
g | 將存儲空間的內容複製到模式空間 |
G | 將存儲空間的內容追加到模式空間 |
n | 讀取下一個輸入行,用下一個命令處理新的行 |
N | 追加下一個輸入行到模板塊後並在兩者間插入新行 |
p | 打印匹配的行 |
P | 打印匹配的第一行 |
q | 退出sed |
r | 從外部文件中讀取文本 |
w | 追加寫文件 |
! | 匹配的逆 |
s/old/new | 用new替換正則表達式old |
= | 打印當前行號 |
sed經常使用的參數:java
sed參數 | 做用 |
-e | 多條件編輯 |
-h | 幫助信息 |
-n | 不輸出不匹配的行 |
-f | 指定sed腳本 |
-V | 版本信息 |
-i | 直接修改原文件 |
sed經常使用的正則表達式匹配:nginx
元字符 | 做用 |
^ | 匹配行的開始。如:/^cat/匹配全部以cat開頭的行 |
$ | 匹配行的結束。如:/cat$/匹配全部以cat結尾的行 |
. | 匹配任一非換行字符。如:/c.t/匹配c後接一個任意字符,而後是t |
* | 匹配零個或任意多個字符。如:/*cat/匹配一串字符後緊跟cat的全部行 |
[] | 匹配指定範圍內的字符。如:/[Cc]at/匹配cat和Cat |
[^] | 匹配指定範圍外的任意單個字符。如:/[^A-Z]/匹配沒有大寫字母的行 |
\(..\) | 保存匹配的字符。如:s/\(love\)able/\1rs/, loveable被替換成lovers |
& | 保存搜索字符用來替換其餘字符。如:s/love/**&**/,love編程**love** |
\< | 錨定單詞的開始。如:/\<cat/匹配包含以cat開頭的單詞的行 |
\> | 錨定單詞的結尾。如:/cat\>/匹配包含以cat結尾的單詞的行 |
[x\{n\} | 重複字符x,m次。如:/o\{5\}/匹配包含5個o的行 |
x\{m,\} | 重複字符x,至少m次。如:/o\{5,\}/匹配至少有5個o的行 |
x\{n,m\} | 重複字符x,至少m次,很少於n次。如:/o\{5,10\}/匹配5到10個o的行 |
[root@kurol ~]# cat sed.txt this is line 1, this is First line this is line 2, the Second line, Empty line followed this is line 4, this is Third line this is line 5, this is Fifth line
sed [option] 'command' file #option 是sed能夠接受的參數 #command 是sed的命令集(一共有25個) #使用-e參數和分號鏈接多編輯命令 #該參數自己只是sed的一個簡單參數,表示將下一個字符串解析爲sed編輯命令 #通常狀況下能夠忽略,可是當sed須要傳遞多個編輯命令時該參數就不能少了
下面的例子演示了將this改成that的同時,還要講line改成LINE,兩個編輯命令前都要使用-e參數,若是有更多的編輯需求,以此類推web
[root@kurol ~]# sed -e 's/this/that/g' -e 's/line/LINE/g' sed.txt that is LINE 1, that is First LINE that is LINE 2, the Second LINE, Empty LINE followed that is LINE 4, that is Third LINE that is LINE 5, that is Fifth LINE
使用分號(;)鏈接兩個都編輯的命令,上面的命令用分號也可達到一樣的效果:正則表達式
[root@kurol ~]# sed 's/this/that/g ; s/line/LINE/g' sed.txt that is LINE 1, that is First LINE that is LINE 2, the Second LINE, Empty LINE followed that is LINE 4, that is Third LINE that is LINE 5, that is Fifth LINE
刪除編程
使用d命令可刪除指定的行:緩存
#將file的第一行刪除後輸出到屏幕
[root@kurol ~]# sed '1d' sed.txt this is line 2, the Second line, Empty line followed this is line 4, this is Third line this is line 5, this is Fifth line
因爲sed默認不修改原文件,若是但願保存修改後的文件則須要用重定向:bash
sed '1d' sed.txt > saved_file
若是想直接修改文件,使用 -i 參數,這樣的方式不會有任何輸出,而是直接修改了源文件編輯器
sed -i '1d' sed.txt
刪除指定範圍的行 :this
刪除1-3行:
[root@kurol ~]# sed '1,3d' sed.txt this is line 4, this is Third line this is line 5, this is Fifth line
刪除第一行當最後一行:
[root@kurol ~]# sed '1,$d' sed.txt [root@kurol ~]# #清空了sed.txt文件
刪除最後一行:
[root@kurol ~]# sed '$d' sed.txt this is line 1, this is First line this is line 2, the Second line, Empty line followed this is line 4, this is Third line
刪除指定範圍之外的行(只保留第五行):
[root@kurol ~]# sed '5!d' sed.txt this is line 5, this is Fifth line
刪除全部包含Empty的行:
[root@kurol ~]# sed '/Empty/d' sed.txt this is line 1, this is First line this is line 4, this is Third line this is line 5, this is Fifth line
刪除空行:
[root@kurol ~]# sed '/^$/d' sed.txt this is line 1, this is First line this is line 2, the Second line, Empty line followed this is line 4, this is Third line this is line 5, this is Fifth line
查找替換
使用s命令可將查找到的匹配文本內容替換成新的文本
s命令用於替換文本
將每行第一個line替換成LINE:
[root@kurol ~]# sed 's/line/LINE/' sed.txt this is LINE 1, this is First line this is LINE 2, the Second line, Empty line followed this is LINE 4, this is Third line this is LINE 5, this is Fifth line
以上只是把每一行的第一個line被替換了,'s/old/new/' 默認狀況下只替換第一次匹配到的內容.
將每行匹配到2個line,並改成LINE:
[root@kurol ~]# sed 's/line/LINE/2' sed.txt this is line 1, this is First LINE this is line 2, the Second LINE, Empty line followed this is line 4, this is Third LINE this is line 5, this is Fifth LINE
s命令利用g選項,能夠完成全部匹配值的替換(全文替換):
[root@kurol ~]# sed 's/line/LINE/g' sed.txt this is LINE 1, this is First LINE this is LINE 2, the Second LINE, Empty LINE followed this is LINE 4, this is Third LINE this is LINE 5, this is Fifth LINE
字符轉換
使用y命令可進行字符轉換,其做用爲將一系列字符逐個地變換爲另一系列字符,基本用法以下:
sed 'y/old/new/' file
該命令會將file中的o轉換爲n、l轉換成e、d轉換成w
注意轉換字符和被轉換字符的長度要相等,不然sed沒法執行
將數字1轉換爲A,2轉換爲B,4轉換爲D,5轉換爲E:
[root@kurol ~]# sed 'y/1245/ABDE/' sed.txt this is line A, this is First line this is line B, the Second line, Empty line followed this is line D, this is Third line this is line E, this is Fifth line
插入文本
使用i或a命令插入文本,其中i表明在匹配行以前插入,而a表明在匹配行以後插入
使用i在第二行前插入文本:
[root@kurol ~]# sed '2 i Insert' sed.txt this is line 1, this is First line Insert this is line 2, the Second line, Empty line followed this is line 4, this is Third line this is line 5, this is Fifth line
使用a在第二行後插入文本:
[root@kurol ~]# sed '2 a Insert' sed.txt this is line 1, this is First line this is line 2, the Second line, Empty line followed Insert this is line 4, this is Third line this is line 5, this is Fifth line
在匹配行的上一行插入文本:
[root@kurol ~]# sed '/Second/i\Insert' sed.txt this is line 1, this is First line Insert this is line 2, the Second line, Empty line followed this is line 4, this is Third line this is line 5, this is Fifth line
讀入文本
使用r命令可從其餘文件中讀取文本,並插入匹配行以後
將/etc/passwd 中的內容讀出放到sed.txt空號以後
[root@kurol ~]# sed '/^$/r /etc/passwd' sed.txt this is line 1, this is First line this is line 2, the Second line, Empty line followed root:x:0:0:root:/root:/bin/bash ...... nginx:x:498:499:Nginx web server:/var/lib/nginx:/sbin/nologin this is line 4, this is Third line this is line 5, this is Fifth line
打印
使用p命令可進行打印,這裏使用sed命令時必定要加-n參數,表示不打印不要緊的行。從以前的例子能夠看出,因爲sed的工做原理是基於行的,所以每次都有大量的輸出。但是這些輸出中有一些咱們並不須要看到的,而只須要輸出匹配的行或者處理過的行就行了。簡單來講,打印操做是刪除操做的「逆操做」。
打印出文件中指定的行:
[root@kurol ~]# sed -n '1p' sed.txt this is line 1, this is First line
將the替換成THE,sed實際處理了第二行,其餘幾行因爲沒有匹配因此並未真正處理,可是sed的工做原理是基於流的,因此全部流過的行都打印出來了:
[root@kurol ~]# sed 's/the/THE/' sed.txt this is line 1, this is First line this is line 2, THE Second line, Empty line followed this is line 4, this is Third line this is line 5, this is Fifth line
使用p命令,則只打印實際處理過的行,簡化了輸出(使用-n參數):
[root@kurol ~]# sed -n 's/the/THE/p' sed.txt this is line 2, THE Second line, Empty line followed
寫文件
sed自己默認並不改寫原文件,而只是對緩存區的文本作了修改並輸出到屏幕。因此想保存文件,除了以前提到的兩種方法外(使用重定向或-i參數),還可使用w命令將結果保存到外部指定文件。
[root@kurol ~]# sed -n '1,2 w output' sed.txt [root@kurol ~]# #這裏沒有任何輸出,由於輸出被重定向到output文件了 [root@kurol ~]# cat output this is line 1, this is First line this is line 2, the Second line, Empty line followed
sed腳本
在平日的工做中,須要按期對一些文件作分析操做,這種例行的工做每每有必定「標準化」 的操做,好比說先去除文件中全部的空行,而後再所有替換某些字符等,這種過程相似於生產線上程式化的流水做業。事實上,能夠把這些動做靜態化地寫到某個文件中,而後調用sed命令並使用-f參數指定該文件,這樣就能夠將一系列動做「裝載」並應用於指定文件中,這無疑加快了工做效率,這種文件就是sed腳本。
如,建立sed.rules腳本文件,該sed腳本的做用是將全文的this改成THAT,並刪除全部空號
[root@kurol ~]# cat sed.rules s/this/THAT/g /^$/d [root@kurol ~]# sed -f sed.rules sed.txt #使用-f參數指定該腳本應用於sed.txt THAT is line 1, THAT is First line THAT is line 2, the Second line, Empty line followed THAT is line 4, THAT is Third line THAT is line 5, THAT is Fifth line