參考網頁: 
[轉貼] SED單行腳本快速參考 @ 胖虎的祕密基地 :: 痞客邦 PIXNET :: 
sed 工具 
阿旺的 Linux 開竅手冊 
 sed, a stream editor Examples
Sed – An Introduction and Tutorial 
1. 刪除空白行 
# sed -i ‘/^$/d’ testfile 
# sed -i ‘/./!d’ testfile 
2. 刪除第一行空行後的所有內容 
# sed -i ‘/^$/q’ testfile 
3. 刪除第一行空行之前的所有內容 
# sed -i ‘1,/^$/d’ testfile[@more@]4. 刪除含 pattern 的行 
# sed -i ‘/pattern/d’ testfile 
# cat /tmp/testfile 
1 
2 
3 
4 
5 
# sed -i ‘/2/’,’/4/d’ /tmp/testfile 
# cat /tmp/testfile 
1 
5 
5. 刪除文件中開頭的10行 
# sed -i ‘1,10d’ testfile 
6. 刪除文件中的最後一行 
# sed -i ‘$d’ testfile 
7. 顯示 8~12 行 
# sed -n ‘8,12p’ testfile 
8. 只顯示符合 pattern 的行 
# sed -n ‘/pattern/p’ testfile 
# sed ‘/pattern/!d’ testfile 
# grep pattern testfile 
9. 不顯示符合 pattern 的行 
# sed -n ‘/pattern/!p’ testfile 
# sed ‘/pattern/d’ testfile 
# grep -v pattern testfile 
10. 一次全部更換多個符合的 pattern 
# sed -i ‘s/mysql/red/g;s/php/black/g’ testfile 
11. 在每一行前面插入 5 個空白 
# sed -i ‘s/^/ /’ testfile 
12. 更換指定行(n)符合的字串 
# sed -i ‘ns/php/red/’ testfile 
13. 在指定行之前插入 
# sed -i ‘2i 1234567890’ testfile 
14. 在指定行之後插入 
# sed -i ‘2a 1234567890’ testfile 
15. 在最後一行插入 
# sed -i ‘$a 1234567890’ testfile 
16. 字串取代 
# sed -i ‘s/^(anonymous_enable=).*$/1”NO/’ /etc/vsftpd/vsftpd.conf 
 # sed -i ‘s/^(SELINUX=).*$/1”disabled/’ /etc/selinux/config
17. 字串取代 
# sed -i ‘/foo/ s//bar/g’ testfile 
18. 字串取代 指定行範圍 
# sed -i ‘34,38 s/ACCEPT/DROP/’ /etc/ufw/before.rules
19. 取出 IP 
# ifconfig eth0 
 inet addr:192.168.1.12 Bcast:192.168.1.255 Mask:255.255.255.0 
# ifconfig eth0 | grep ‘inet ‘ | sed ‘s/^.*inet addr://g’ | sed ‘s/ *Bcast.*$//g’ 
192.168.1.12 
20. 多個指令 
# sed -i ‘s/123/234/; s/四忠/四義/’ list 
21. 將編輯命令放在檔案之中 
# cat sedscr 
s/123/234/ 
s/四忠/四義/ 
# sed -i -f sedscr list
22. 刪除找到 %post 後的所有行數
# sed -i ‘/%post/ ,$d’ /tmp/anaconda-ks.cfg
23. 找到字串的後面插入一行
在 #PIDFILE 後面插入一行
# sed -i ‘/#PIDFILE/ a PIDFILE=/var/chroot/bind9/var/run/named/named.pid’ /etc/init.d/bind9
24. 多重取代
# sed -i -e ‘s/123/234/’ -e ‘s/四忠/四義/’ list
25. 刪除最後幾個字元
# sed -i ‘s/…$//’ testfile
26. 在每一行後面插入一行空白行
# sed -i G testfile
27. 在最後一個欄位插入字串
# sed -i ‘s/$/@smail.ilc.edu.tw/’ class3