在linux的學習和使用中,sed,awk,grep的用法的重要性不言而喻,本文從以下幾個方面小結了sed的用法:
1、sed的含義
sed 是stream editor;就是流編輯器,和我們用到的vi是不同的,vi是全屏編輯器。而且sed是逐行將文件內容取入到內存,進過sed中指定命令進行處理,然後輸出到屏幕的。
2、sed的用法及格式
sed [options] 'StartAdd,EndAddCommand' files,...
[options] 指的是sed的選項。可以省略
StartAdd,EndAdd 指的sed中地址的表示方法
Command指的是sed的處理命令。
files,..可以是多個,是sed的處理對象。即把那個文件放入內存,然後逐行處理的。
3、地址的表示方式
'1,3p' passwd #表示的含義是,打印passwd文件1到第三行。絕對行數表示法
[root@localhost testdir]# sed -n '1,3p' passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
'1,+3p' passwd #表示的含義是:打印第一行及後面的三行的內容。相對行數表示法
[root@localhost testdir]# sed -n '1,+3p' passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
'36,$p' passwd #表示打印從38行到最後一行的passwd的內容 $表示最後一行
[root@localhost testdir]# sed -n '36,$p' passwd
user1:x:504:504::/home/user1:/bin/bash
user2:x:505:505::/home/user2:/bin/bash
user3:x:506:506::/home/user3:/bin/bash
'/pattern/Command' #表示匹配到patern的行打印,pattern可以是正則表達式
[root@localhost testdir]# sed -n '/^root/p' passwd
root:x:0:0:root:/root:/bin/bash
rootbisu:x:500:500:centos_X641:/home/rootbisu:/bin/bash
'/pattern1/,/pattern2/Command' 用command處理/pattern1/首次匹配到的行,到/pattern2/首次匹配到的行的內容
[root@localhost testdir]# sed -n '/^\<root\>/,/^\<bin\>/p' passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
sed '3p' passwd # 表示打印passwd的第三行
[root@localhost testdir]# sed -n '3p' passwd
daemon:x:2:2:daemon:/sbin:/sbin/nologin
4、sed中命令及參數的用法
sed既然作爲一個流編輯器,那麼他的主要命令功能應該體現在編輯上。常見的編輯操作有,打印,刪除,替換,添加等操作。
d:表示刪除符合條件的行
p:表示打印符合條件的行
a:表示在符合條件的行後,格式:a\string 追加新的內容a是append # a後是什麼輸出什麼。 [root@localhost testdir]# sed '/^\<root\>/a\appendtest' passwd
root:x:0:0:root:/root:/bin/bash
appendtest
i:insert ,表在符合條件的行前,插入新的string.格式i\string
[root@localhost testdir]# sed '/^\<root\>/i\appendtest' passwd
appendtest
root:x:0:0:root:/root:/bin/bash
r:read ,rfile表示在符合條件的行的後面將read的file的內容附加
[root@localhost testdir]# sed '/^\<root\>/rgrep.txt' passwd
root:x:0:0:root:/root:/bin/bash
This is root
The user is mroot
rooter is a dog's name.
Mrooter is not a word
bin:x:1:1:bin:/bin:/sbin/nologin
w:write,wfile表示將符合條件的行寫至file中
[root@localhost testdir]# sed -n '/^\<root\>/wgrep.txt' passwd
[root@localhost testdir]# cat grep.txt
root:x:0:0:root:/root:/bin/bash
s表示查找並替換 # 替換第一次匹配到的項
[root@localhost testdir]# sed 's/root/ROOT/' passwd
ROOT:x:0:0:root:/root:/bin/bash
# 加上參數g表示替換所有匹配到項
[root@localhost testdir]# sed 's/root/ROOT/g' passwd
ROOT:x:0:0:ROOT:/ROOT:/bin/bash
5、常用的參數:
-n:表示靜默模式,只輸出處理了的行,其他的不輸出
-i:會將command的結果作用到被處理的文件上
-r:表示pattern中可以使用擴展正則表達式
-e: 表示可以使用多個命令。sed -e '10,11p' -e '12,13d'
6、sed用法的小結及注意事項
sed的用法非常關鍵,需要牢記,sed的用法及格式,地址的表示方法,和常用的命令,以及命令的參數,此處還有需要注意的細節是,可以在sed中使用後項引用。
有一個文本內容如下:
he like his like
she love her love
he like his love
she love his like
有一個需求,想把like變成liker,想把love變成lover應該怎麼處理?這個時候可以使用後項引用了。
[root@localhost testdir]# sed 's/\(l..e\)/\1r/g' test
he liker his liker
she lover her lover
he liker his lover
she lover her liker
[root@localhost testdir]# sed 's/\(l..e\)/\(l,,e\)r/g' test
he (l,,e)r his (l,,e)r
she (l,,e)r her (l,,e)r
he (l,,e)r his (l,,e)r
she (l,,e)r her (l,,e)r
注意,我們發現,'s/string1/string2/g' 其中string可以是正則表達式,如果是用了-r選項,那麼還可以使用擴展正則表達式,但是string2是不能使用正則表達式的。