sed是一種流編編器,它是文本處理中非常中的工具,能夠完美的配合正則表達式便用,功物能不同凡響。
處理時,把當前處理的行存儲在臨時緩衝區中,稱爲”模式空間”( oattern space),接看用sed命令處理緩衝區中的內容,處理成後,把緩衝區的內容送往屏幕顯示。
接着理下一行,這樣不斷重複,直到文件末。文件內容沒有改改變,除非使用了寫入的命令,將內容更新。
定址用於決定對哪些行進行編輯。地址的形式可以是數字、正則表達式、或二者的結合。如果沒有指定地址,sed將處理輸入文件的所有行。
sed命令功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
命令 功能
a\ 在當前行後添加一行或多行。多行時除最後一行外,每行末尾需用“\”續行
c\ 用此符號後的新文本替換當前行中的文本。多行時除最後一行外,每行末尾需用"\"續行
i\ 在當前行之前插入文本。多行時除最後一行外,每行末尾需用"\"續行
d 刪除行
h 把模式空間裏的內容複製到暫存緩衝區
H 把模式空間裏的內容追加到暫存緩衝區
g 把暫存緩衝區裏的內容複製到模式空間,覆蓋原有的內容
G 把暫存緩衝區的內容追加到模式空間裏,追加在原有內容的後面
l 列出非打印字符
p 打印行
n 讀入下一輸入行,並從下一條命令而不是第一條命令開始對其的處理
q 結束或退出 sed
r 從文件中讀取輸入行
! 對所選行以外的所有行應用命令
s 用一個字符串替換另一個
g 在行內進行全局替換
w 將所選的行寫入文件
x 交換暫存緩衝區與模式空間的內容
y 將字符替換爲另一字符(不能對正則表達式使用y命令)
|
sed常見命令參數
1
2
3
4
5
6
7
|
p==print
d:delete
=:打印匹配行的行號
-n 取消默認的完整輸出,只要需要的
-e 允許多項編輯
-i 修改文件內容
-r 不需要轉義
|
1
|
注意:& 符號在 sed 命令中代表上次匹配的結果
|
常用的命令展示
sed擅長行輸出,附源文件:
1
2
3
4
5
6
7
8
9
10
11
12
|
hhh pts /1 192.168.25.1 Sat Jun 30 22:04 still logged in
reboot system boot 2.6.32-358.el6.i Sat Jun 30 22:04 - 22:43 (00:38)
omc pts /0 192.168.25.1 Sat Jun 30 20:16 - down (00:00)
reboot system boot 2.6.32-358.el6.i Sat Jun 30 19:38 - 20:16 (00:37)
root pts /1 192.168.25.1 Sat Jun 30 12:20 - down (00:55)
root pts /0 192.168.25.1 Sat Jun 30 11:53 - down (01:22)
reboot system boot 2.6.32-358.el6.i Sat Jun 30 11:52 - 13:15 (01:23)
root pts /0 192.168.25.1 Sat Jun 30 05:40 - down (02:51)
reboot system boot 2.6.32-358.el6.i Sat Jun 30 05:38 - 08:32 (02:54)
root pts /0 192.168.25.1 Fri Jun 29 21:01 - down (06:21)
wtmp begins Tue Jun 9 03:57:56 2015
|
只打印第三行
1
|
sed -n '3p' /var/log/yum .log
|
只查看文件的第3行到第9行
1
|
sed -n '3,9p' /var/log/yum .log
|
過濾特定字符串,顯示正行內容
1
|
sed -n '/root/p' yum.log
|
顯示包含"hhh"的行到包含"omc"的行之間的行
1
|
sed -n '/hhh/,/omc/p' yum.log
|
打印1-5行,並顯示行號
1
|
sed -n -e '1,5p' -e '=' yum.log
|
僅僅顯示匹配字符串的行號
1
|
sed -n '/root/p' yum.log
|
打印匹配行的內容和符號【相當於後面又根據關鍵詞查詢了一次】
1
|
sed -n -e '/root/p' -e '/root/=' yum.log
|
用world 替換yum.log文件中的root【真實寫入】
1
2
3
4
5
|
sed -i 's/root/world/g' yum.log
==> sed -i 's#hhh#HHHH#g' h.txt 與上同
s==search 查找並替換
g==global 全部替換
-i: implace
|
打印最後一行
在文件第一行添加happy,文件結尾添加new year
1
2
|
sed -e '1i happy' -e '$a new year' yum.log 【界面顯示】
sed -i -e '1i happy' -e '$a new year' yum.log 【真實寫入文件】
|
在文件第一行和第四行的每行下面添加hahaha
1
|
sed '1,4i hahaha' yum.log
|
& 符號在sed命令中代表上次匹配的結果
1
|
sed 's/world/hello_&/g' yum.log
|
刪除第3到第9行,只是不顯示而已
1
|
sed '3,9d' /var/log/yum .log
|
刪除包含"hhh"的行到包含"omc"的行之間的行
1
|
sed '/hhh/,/omc/d' yum.log
|
刪除包含"omc"的行到第十行的內容
1
|
sed '/omc/,10d' yum.log
|
備註:範圍可以用數字、正則表達式、或二者的組合表示
顯示中5到10行裏匹配root,把行內所有的root替換爲FTL,並打印到屏幕上
1
2
|
sed '5,10 s/root/FTL/g' yum.log【僅顯示用】
sed -i sed '5,10 s/root/FTL/g' yum.log【-i 會真正替換信息】
|
-e是編輯命令,用於多個編輯任務
1
2
|
刪除1到5行後,替換reboot爲 shutdown
sed -e '1,5d' -e '=' -e 's/reboot/shutdown/g' yum.log
|
打印3到6行的內容,並匹配reboot替換爲shutdown
1
|
sed -n -e '3,6p' -e 's/reboot/shutdown/g' yum.log
|
-r命令是讀命令。sed使用該命令將一個文本文件中的內容加到當前文件的特定位置上
1
2
|
[root@localhost log] # cat test.txt
[root@localhost log] # sed '/root/ r /var/log/test.txt' yum.log 【yum.log 匹配root後讀入text.txt的內容在匹配內容之下,僅顯示用】
|
-w是寫入命令
1
2
3
|
sed '/root/ w /home/omc/root.txt' yum.log 【yum.log匹配root後寫入root.txt',真實寫入】
sed -n '/root/ w /home/omc/root.txt' yum.log 【添加-n參數後,不打印源文件】
ll /home/omc/root .txt
|
打印1-5行,並顯示行號
1
|
sed -n -e '1,5p' -e '=' yum.log
|
sed的正則匹配
正則匹配IP和子網掩碼
1
2
3
4
|
[root@localhost log] # ifconfig |sed -n '2p'
[root@localhost log] # ifconfig |sed -n '2p' | sed -r 's#.*r:(.*) B.*k:(.*)#\1 \2#g'
(.*)表示匹配的項,之後可以用\1取出第一個括號內匹配的內容,\2取出第二個括號內匹配的內容
|
配合find命令的使用
1
2
3
4
|
find . -name "*.txt" | xargs sed -i 's/hhhh/\hHHh/g'
find . -name "*.txt" | xargs sed -i 's#hhhh#hHHh#g'
find . -name "*.txt" - exec sed -i 's/hhhh/\hHHh/g' {} \;
find . -name "*.txt" | xargs cat
|
sed錯誤使用
1
2
|
sed 's#,# #h' h.txt | cut -d " " -f 3,5 h.txt <-- 錯誤的用法,前面已經處理了,後面又用 cut 查看h.txt文件
sed 's#,# #h' h.txt | cut -d " " -f 3,5 --> 正確的用法
|