在 Linux 系統中,sed
(Stream Editor)是一個非常強大且靈活的文本處理工具。它不僅可以用于簡單的文本替換、刪除和插入操作,還能實現復雜的文本轉換任務。
📌 一、什么是 sed?
sed
是一個基于模式匹配對文本進行過濾或修改的流編輯器。它可以:
- 替換文本內容
- 刪除指定行
- 插入或追加新內容
- 打印特定行
- 使用正則表達式進行高級匹配
- 多條命令組合處理
與交互式編輯器(如 vim
)不同,sed
是非交互式的,適合在腳本中自動執行批量文本處理任務。
🔧 二、sed 基本語法
sed [選項] '命令' 文件名
示例:
sed 's/old/new/' file.txt
表示將 file.txt
中每一行的第一個 old
替換為 new
。
🧱 三、常用命令格式說明
命令 | 含義 |
---|---|
s/pattern/replacement/flags | 替換命令(最常用) |
d | 刪除匹配的行 |
p | 打印匹配的行(常與 -n 搭配使用) |
i\text | 在匹配行前插入文本 |
a\text | 在匹配行后追加文本 |
c\text | 替換整行內容 |
y/abc/xyz/ | 字符替換(一一對應) |
📚 四、實戰示例
? 示例 1:替換第一個匹配項(s
命令)
📄 原始內容(input.txt):
hello world
hello there
🔧 使用命令:
sed 's/hello/hi/' input.txt
? 輸出結果:
hi world
hi there
只替換每行中第一個出現的
hello
。
? 示例 2:替換所有匹配項(加 g
標志)
📄 原始內容(input.txt):
hello world hello
hello again
🔧 使用命令:
sed 's/hello/hi/g' input.txt
? 輸出結果:
hi world hi
hi again
g
表示全局替換,替換該行中所有匹配項。
? 示例 3:刪除某一行(d
命令)
📄 原始內容(input.txt):
apple
banana
orange
🔧 使用命令:
sed '2d' input.txt
? 輸出結果:
apple
orange
刪除第 2 行(
banana
)。
? 示例 4:刪除包含某個關鍵詞的行(/pattern/d
)
📄 原始內容(input.txt):
error: failed to connect
success: connection ok
error: timeout
🔧 使用命令:
sed '/error/d' input.txt
? 輸出結果:
success: connection ok
刪除所有包含
error
的行。
? 示例 5:打印特定行(-n + p
)
📄 原始內容(input.txt):
line one
line two
line three
🔧 使用命令:
sed -n '2p' input.txt
? 輸出結果:
line two
只打印第 2 行。
-n
阻止默認輸出,p
顯式打印指定行。
? 示例 6:在匹配行前插入內容(i\
)
📄 原始內容(input.txt):
header line
data line
footer line
🔧 使用命令:
sed '/data/i\--- INSERTED LINE ---' input.txt
? 輸出結果:
header line
--- INSERTED LINE ---
data line
footer line
在包含
data
的行前面插入新內容。
? 示例 7:在匹配行后追加內容(a\
)
📄 原始內容(input.txt):
start section
some data
end section
🔧 使用命令:
sed '/end/a\--- APPENDED LINE ---' input.txt
? 輸出結果:
start section
some data
end section
--- APPENDED LINE ---
在包含
end
的行后面追加新內容。
? 示例 8:替換整行內容(c\
)
📄 原始內容(input.txt):
old config value
new config value
🔧 使用命令:
sed '/old/c\updated config value' input.txt
? 輸出結果:
updated config value
new config value
替換整行內容。
? 示例 9:字符替換(y/abc/xyz/
)
📄 原始內容(input.txt):
abc def ghi
🔧 使用命令:
sed 'y/abc/xyz/' input.txt
? 輸出結果:
zyx def ghi
將
a->z
,b->y
,c->x
一一替換。
? 示例 10:刪除空行(/^$/d
)
📄 原始內容(input.txt):
This is a line.Another line.
🔧 使用命令:
sed '/^$/d' input.txt
? 輸出結果:
This is a line.
Another line.
刪除空白行。
? 示例 11:刪除注釋行(以 # 開頭)
📄 原始內容(input.txt):
# comment line
valid line
# another comment
🔧 使用命令:
sed '/^#/d' input.txt
? 輸出結果:
valid line
刪除以
#
開頭的注釋行。
? 示例 12:給每行加行號(結合兩個 sed 命令)
📄 原始內容(input.txt):
apple
banana
cherry
🔧 使用命令:
sed '=' input.txt | sed 'N;s/\n/ /'
? 輸出結果:
1 apple
2 banana
3 cherry
給每行加上行號。
? 示例 13:提取兩個關鍵字之間的內容
📄 原始內容(input.txt):
start_key
line one
line two
end_key
another line
🔧 使用命令:
sed -n '/start_key/,/end_key/p' input.txt
? 輸出結果:
start_key
line one
line two
end_key
提取從
start_key
到end_key
之間的段落。
? 示例 14:變量替換(配置文件更新)
📄 原始內容(config.ini):
server=127.0.0.1
port=8080
enabled=true
🔧 使用命令:
sed 's/^port=.*/port=9090/' config.ini
? 輸出結果:
server=127.0.0.1
port=9090
enabled=true
替換配置文件中的端口號。
💾 五、保存修改
默認情況下,sed
不會修改原文件,只是輸出結果。如果需要保存更改,可以結合重定向:
sed 's/old/new/g' input.txt > output.txt
或者使用 -i
參數直接修改原文件(慎用):
sed -i 's/old/new/g' input.txt
你可以添加 .bak
后綴來備份原文件:
sed -i.bak 's/old/new/g' input.txt
🧪 六、總結
sed
是 Linux 下非常實用的文本處理利器,掌握其基本命令和正則表達式技巧,可以大幅提升你在日常運維、日志分析、自動化腳本編寫等方面的效率。
記住一句話:
grep
查找,sed
修改,awk
分析 —— 這就是 Linux 文本處理三劍客!