shell指令碼sed工具
阿新 • • 發佈:2020-11-02
一、sed介紹
- Sed是文字處理工具,依賴於正則表示式,可以讀取文字內容,根據指定條件對資料進行新增、刪除、替換等操作,廣泛應用於shell指令碼,以完成自動化處理任務。
- Sed在處理資料時預設不直接修改原始檔,而是把當前處理的行儲存在臨時緩衝區中,所有指令都在緩衝區中操作,處理完成後,把緩衝區內容預設輸出到螢幕,接著處理下一行內容,這樣不斷重複,直到檔案末尾,檔案本身內容並沒有做任何改變。
二、sed的工作流程
- 讀取
- sed 從輸入流(檔案、管道、標準輸入)中讀取一行內容並存儲到臨時的緩衝區中(又稱模式空間,pattern space)。
- 執行
- 預設情況下,所有的 sed 命令都在模式空間中順序地執行,除非指定了行的地址,否則 sed 命令將會在所 有的行上依次執行
- 顯示
- 傳送修改後的內容到輸出流。再發送資料後,模式空間將會被清空。
三、sed命令的常見用法
- 通常情況下呼叫 sed 命令有兩種格式
sed[選項] '操作' 引數
sed [選項] -f 指令碼檔案 引數
3.1、sed命令選項
選項 | 解釋 |
-e或-expression= | 表示用指定命令或者指令碼來處理輸入的文字檔案 |
-f 或-file= | 表示用指定的指令碼檔案來處理輸入的文字檔案 |
-h或一help | 顯示幫助 |
-n、-quiet 或silent | 表示僅顯示處理後的結果 |
-i | 直接編輯文字檔案,直接編輯原始檔 |
3.2、操作命令
操作 | 解釋 |
a | 增加,在當前行下面增加一行指定內容 |
c | 替換,將選定行替換為指定內容 |
d | 刪除,刪除選定的行 |
i | 插入,在選定行上面插入一行指定內容 |
p | 列印,如果同時指定行,表示列印指定行;如果不指定行,則表示列印所有內容;如果有非列印字元,則以ASCII碼輸出。其通常與“-n”選項一起使用 |
s | 替換,替換指定字元 |
y | 字元轉換 |
3.3、sed 常用示例
3.3.1、p - 輸出符合條件的文字
- p 表示正常輸出
[root@localhost ~]# sed -n 'p' test.txt '輸出所有內容,等同於 cat test.txt'
...省略內容
[root@localhost ~]# sed -n '3p' test.txt '輸出第 3 行'
...省略內容
[root@localhost ~]# sed -n '3,5p' test.txt '輸出第 3~5 行'
...省略內容
[root@localhost ~]# sed -n 'p;n' test.txt '輸出所有奇數行,n 表示讀入下一行資料'
...省略內容
[root@localhost ~]# sed -n 'n;p' test.txt '輸出所有偶數行,n 表示讀入下一行資料'
...省略內容
[root@localhost ~]# sed -n '1,5{p;n}' test.txt '輸出第 1~5 行之間的奇數行(第 1、3、5 行)'
...省略內容
[root@localhost ~]# sed -n '10,${n;p}' test.txt '輸出第 10 行至檔案尾之間的偶數行'
...省略內容
sed 命令與正則表示式結合使用
- sed 命令與正則表示式結合使用時格式略有不同,正則表示式以 “ / ” 包圍
[root@localhost ~]# sed -n '/the/p' test.txt '輸出包含the 的行'
...省略部分內容
[root@localhost ~]# sed -n '4,/the/p' test.txt '輸出從第 4 行至第一個包含 the 的行'
...省略部分內容
[root@localhost ~]# sed -n '/the/=' test.txt '輸出包含the 的行所在的行號,等號(=)用來輸出行號'
...省略部分內容
[root@localhost ~]# sed -n '/^PI/p' test.txt '輸出以PI 開頭的行'
...省略部分內容
[root@localhost ~]# sed -n '/[0-9]$/p' test.txt '輸出以數字結尾的行'
...省略部分內容
[root@localhost ~]# sed -n '/\<wood\>/p' test.txt '輸出包含單詞wood 的行,\<、\>代表單詞邊界'
...省略部分內容
3.3.2、d - 刪除符合條件的文字
下面命令中 nl 命令用於計算檔案的行數,結合該命令可以更加直觀地檢視到命令執行的結果
[root@localhost ~]# nl test.txt | sed '3d' '刪除第 3 行'
...省略內容
[root@localhost ~]# nl test.txt | sed '3,5d' '刪除第 3~5 行'
...省略內容
[root@localhost ~]# nl test.txt | sed '/cross/d' '刪除包含cross 的行'
...省略內容
[root@localhost ~]# nl test.txt | sed '/cross/! d' '刪除不包含cross 的行'
...省略內容
[root@localhost ~]# sed '/^[a-z]/d' test.txt '刪除以小寫字母開頭的行'
...省略內容
[root@localhost ~]# sed '/\.$/d' test.txt '刪除以"."結尾的行'
...省略內容
[root@localhost ~]# sed '/^$/d' test.txt '刪除所有空行'
...省略內容
[root@localhost ~]# sed -e '/^$/{n;/^$/d}' test.txt '刪除重複的空行,即連續的空行只保留一個,效果與“cat -s test.txt”相同,n 表示讀下一行資料'
...省略內容
3.3.3、s - 替換符合條件的文字
- 使用 sed 命令進行替換操作時需要用到 s(字串替換)、c(整行/整塊替換)、y(字元轉換)命令選項
[root@localhost ~]# sed 's/the/THE/' test.txt '將每行中的第一個the 替換為 THE '
[root@localhost ~]# sed 's/l/L/2' test.txt '將每行中的第 2 個l 替換為L '
[root@localhost ~]# sed 's/the/THE/g' test.txt '將檔案中的所有the 替換為THE'
[root@localhost ~]# sed 's/o//g' test.txt '將檔案中的所有o 刪除(替換為空串)'
[root@localhost ~]# sed 's/^/#/' test.txt '在每行行首插入#號'
[root@localhost ~]# sed '/the/s/^/#/' test.txt '在包含the 的每行行首插入#號'
[root@localhost ~]# sed 's/$/EOF/' test.txt '在每行行尾插入字串EOF'
[root@localhost ~]# sed '3,5s/the/THE/g' test.txt '將第 3~5 行中的所有the 替換為 THE'
[root@localhost ~]# sed '/the/s/o/O/g' test.txt '將包含the 的所有行中的o 都替換為 O'
3.3.4、遷移符合條件的文字
- H,複製到剪貼簿
- g、G,將剪貼簿中的資料覆蓋/追加至指定行
- w,儲存為檔案
- r,讀取指定檔案
- a,追加指定內容
[root@localhost ~]# sed '/the/{H;d};$G' test.txt '將包含the 的行遷移至檔案末尾,{;}用於多個操作'
[root@localhost ~]# sed '1,5{H;d};17G' test.txt '將第 1~5 行內容轉移至第 17 行後'
[root@localhost ~]# sed '/the/w out.file' test.txt '將包含the 的行另存為檔案out.file '
[root@localhost ~]# sed '/the/r /etc/hostname' test.txt '將檔案/etc/hostname 的內容新增到包含the 的每行以後'
[root@localhost ~]# sed '3aNew' test.txt '在第 3 行後插入一個新行,內容為 New '
[root@localhost ~]# sed '/the/aNew' test.txt '在包含the 的每行後插入一個新行,內容為 New'
[root@localhost ~]# sed '3aNew1\nNew2' test.txt '在第 3 行後插入多行內容,中間的\n 表示換行'
3.3.5、f - 使用指令碼編輯檔案
[root@localhost ~]# sed '1,5{H;d};17G' test.txt '將第 1~5 行內容轉移至第 17 行後'
'以上操作可以改用指令碼檔案方式:'
[root@localhost ~]# vim abc.list '編輯指令放到/abc.list中'
1,5H
1,5d
17G
[root@localhost ~]# sed -f abc.list test.txt '使用abc.list檔案指令編輯test.txt檔案'