- 常規編輯器編輯困難的文本。
- 太過龐大的文本,使用常規編輯器難以勝任(如vi一個幾百兆的文件)。
- 有規律的文本修改,加快文本處理速度(比如說全文替換)。
sed命令 | 作用 |
a | 在匹配行后面加入文本 |
c | 字符轉換 |
d | 刪除行 |
D | 刪除第一行 |
i | 在匹配行前面接入文本 |
h |
復制模板塊的內容到存儲空間 |
H | 追加模板塊的內容到存儲空間 |
g | 將存儲空間的內容復制到模式空間 |
G | 將存儲空間的內容追加到模式空間 |
n | 讀取下一個輸入行,用下一個命令處理新的行 |
N | 追加下一個輸入行到模板塊后並在二者間插入新行 |
p | 打印匹配的行 |
P | 打印匹配的第一行 |
q | 退出sed |
r | 從外部文件中讀取文本 |
w | 追加寫文件 |
! | 匹配的逆 |
s/old/new | 用new替換正則表達式old |
= | 打印當前行號 |
sed常用的參數:
sed參數 | 作用 |
-e | 多條件編輯 |
-h | 幫助信息 |
-n | 不輸出不匹配的行 |
-f | 指定sed腳本 |
-V | 版本信息 |
-i | 直接修改原文件 |
sed常用的正則表達式匹配:
元字符 | 作用 |
^ | 匹配行的開始。如:/^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參數,如果有更多的編輯需求,以此類推
[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默認不修改原文件,如果希望保存修改后的文件則需要用重定向:
sed '1d' sed.txt > saved_file
如果想直接修改文件,使用 -i 參數,這樣的方式不會有任何輸出,而是直接修改了源文件
sed -i '1d' sed.txt
刪除指定范圍的行 :
刪除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