溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

sed工具在Shell中的使用方法

發(fā)布時間:2020-05-28 17:54:49 來源:億速云 閱讀:197 作者:鴿子 欄目:系統(tǒng)運維

一、sed介紹

1. sed用來干什么的

sed是Stream Editor(流編輯器)的縮寫,簡稱流編輯器;用來處理文件的。

2. sed如何處理文件

sed是一行一行讀取文件內(nèi)容并按照要求進行處理,把處理后的結(jié)果輸出到屏幕。

  1. 首先sed讀取文件中的一行內(nèi)容,把其保存在一個臨時緩存區(qū)中(也稱為模式空間)
  2. 然后根據(jù)需求處理臨時緩沖區(qū)中的行,完成后把該行發(fā)送到屏幕上

總結(jié):

  1. 由于sed把每一行都存在臨時緩沖區(qū)中,對這個副本進行編輯,所以不會直接修改原文件
  2. Sed主要用來自動編輯一個或多個文件;簡化對文件的反復操作,對文件進行過濾和轉(zhuǎn)換操作

二、sed使用方法介紹

sed常見的語法格式有兩種,一種叫命令行模式,另一種叫腳本模式。

1. 命令行格式

1)語法格式

sed  [options]    '處理動作'  文件名

  • 常用選項
選項說明備注
-e進行多項(多次)編輯
-n取消默認輸出不自動打印模式空間
-r使用擴展正則表達式
-i原地編輯(修改源文件)
-f指定sed腳本的文件名
  • 常見處理動作

丑話說在前面:以下所有的動作都要在單引號里,

動作說明備注
'p'打印
'i'在指定行之前插入內(nèi)容類似vim里的大寫O
'a'在指定行之后插入內(nèi)容類似vim里的小寫o
'c'替換指定行所有內(nèi)容
'd'刪除指定行

2)舉例說明

  • 文件準備
# vim a.txt 
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
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
298374837483
172.16.0.254
10.1.1.1
① 對文件進行==增、刪、改、查==操作

語法:sed  選項    '定位+命令'    需要處理的文件

1)打印文件內(nèi)容
[root@server ~]# sed ''  a.txt                      對文件什么都不做
[root@server ~]# sed -n 'p'  a.txt                  打印每一行,并取消默認輸出
[root@server ~]# sed -n '1p'  a.txt                 打印第1行
[root@server ~]# sed -n '2p'  a.txt                 打印第2行
[root@server ~]# sed -n '1,5p'  a.txt               打印1到5行
[root@server ~]# sed -n '$p' a.txt                  打印最后1行
2)增加文件內(nèi)容

i    地址定位的上面插入

a   下面插入

[root@server ~]# sed '$a99999' a.txt                文件最后一行下面增加內(nèi)容
[root@server ~]# sed 'a99999' a.txt                 文件每行下面增加內(nèi)容
[root@server ~]# sed '5a99999' a.txt                文件第5行下面增加內(nèi)容
[root@server ~]# sed '$i99999' a.txt                文件最后一行上一行增加內(nèi)容
[root@server ~]# sed 'i99999' a.txt                 文件每行上一行增加內(nèi)容
[root@server ~]# sed '6i99999' a.txt                文件第6行上一行增加內(nèi)容
[root@server ~]# sed '/^uucp/ihello'                以uucp開頭行的上一行插入內(nèi)容
3)修改文件內(nèi)容

c   替換指定的==整行==內(nèi)容

[root@server ~]# sed '5chello world' a.txt      替換文件第5行內(nèi)容
[root@server ~]# sed 'chello world' a.txt       替換文件所有內(nèi)容
[root@server ~]# sed '1,5chello world' a.txt    替換文件1到5號內(nèi)容為hello world
[root@server ~]# sed '/^user01/c888888' a.txt   替換以user01開頭的行
4)刪除文件內(nèi)容
[root@server ~]# sed '1d' a.txt                         刪除文件第1行
[root@server ~]# sed '1,5d' a.txt                   刪除文件1到5行
[root@server ~]# sed '$d' a.txt                     刪除文件最后一行
② 對文件進行搜索替換操作

語法:sed   選項   's/搜索的內(nèi)容/替換的內(nèi)容/動作'  需要處理的文件

其中,s表示search搜索;斜杠/表示分隔符,可以自己定義;動作一般是打印p和全局替換g

[root@server ~]# sed -n 's/root/ROOT/p' 1.txt 
[root@server ~]# sed -n 's/root/ROOT/gp' 1.txt 
[root@server ~]# sed -n 's/^#//gp' 1.txt 
[root@server ~]# sed -n 's@/sbin/nologin@itcast@gp' a.txt
[root@server ~]# sed -n 's/\/sbin\/nologin/itcast/gp' a.txt
[root@server ~]# sed -n '10s#/sbin/nologin#itcast#p' a.txt 
uucp:x:10:14:uucp:/var/spool/uucp:itcast
[root@server ~]# sed -n 's@/sbin/nologin@itcastheima@p' 2.txt 
注意:搜索替換中的分隔符可以自己指定

[root@server ~]# sed -n '1,5s/^/#/p' a.txt      注釋掉文件的1-5行內(nèi)容
#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
#lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
③ 其他命令
命令解釋備注
r從另外文件中讀取內(nèi)容
w內(nèi)容另存為
&保存查找串以便在替換串中引用和\(\)相同
=打印行號
對所選行以外的所有行應用命令,放到行數(shù)之后'1,5!'
q退出

舉例說明:

r   從文件中讀取輸入行
w   將所選的行寫入文件
[root@server ~]# sed '3r /etc/hosts' 2.txt 
[root@server ~]# sed '$r /etc/hosts' 2.txt
[root@server ~]# sed '/root/w a.txt' 2.txt 
[root@server ~]# sed '/[0-9]{4}/w a.txt' 2.txt
[root@server ~]# sed  -r '/([0-9]{1,3}\.){3}[0-9]{1,3}/w b.txt' 2.txt

!   對所選行以外的所有行應用命令,放到行數(shù)之后
[root@server ~]# sed -n '1!p' 1.txt 
[root@server ~]# sed -n '4p' 1.txt 
[root@server ~]# sed -n '4!p' 1.txt 
[root@server ~]# cat -n 1.txt 
[root@server ~]# sed -n '1,17p' 1.txt 
[root@server ~]# sed -n '1,17!p' 1.txt 

&   保存查找串以便在替換串中引用   \(\)

[root@server ~]# sed -n '/root/p' a.txt 
root:x:0:0:root:/root:/bin/bash
[root@server ~]# sed -n 's/root/#&/p' a.txt 
#root:x:0:0:root:/root:/bin/bash

# sed -n 's/^root/#&/p' passwd   注釋掉以root開頭的行
# sed -n -r 's/^root|^stu/#&/p' /etc/passwd 注釋掉以root開頭或者以stu開頭的行
# sed -n '1,5s/^[a-z].*/#&/p' passwd  注釋掉1~5行中以任意小寫字母開頭的行
# sed -n '1,5s/^/#/p' /etc/passwd  注釋1~5行
或者
sed -n '1,5s/^/#/p' passwd 以空開頭的加上#
sed -n '1,5s/^#//p' passwd 以#開頭的替換成空

[root@server ~]# sed -n '/^root/p' 1.txt 
[root@server ~]# sed -n 's/^root/#&/p' 1.txt 
[root@server ~]# sed -n 's/\(^root\)/#\1/p' 1.txt 
[root@server ~]# sed -nr '/^root|^stu/p' 1.txt 
[root@server ~]# sed -nr 's/^root|^stu/#&/p' 1.txt 

=   打印行號
# sed -n '/bash$/=' passwd    打印以bash結(jié)尾的行的行號
# sed -ne '/root/=' -ne '/root/p' passwd 
# sed -n '/nologin$/=;/nologin$/p' 1.txt
# sed -ne '/nologin$/=' -ne '/nologin$/p' 1.txt

q   退出
# sed '5q' 1.txt
# sed '/mail/q' 1.txt
# sed -r '/^yunwei|^mail/q' 1.txt
[root@server ~]# sed -n '/bash$/p;10q' 1.txt
ROOT:x:0:0:root:/root:/bin/bash

綜合運用:
[root@server ~]# sed -n '1,5s/^/#&/p' 1.txt 
#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
#lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

[root@server ~]# sed -n '1,5s/\(^\)/#\1/p' 1.txt 
#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
#lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
④ 其他選項
-e 多項編輯
-r  擴展正則
-i 修改原文件

[root@server ~]# sed -ne '/root/p' 1.txt -ne '/root/='
root:x:0:0:root:/root:/bin/bash
1
[root@server ~]# sed -ne '/root/=' -ne '/root/p' 1.txt 
1
root:x:0:0:root:/root:/bin/bash

在1.txt文件中的第5行的前面插入“hello world”;在1.txt文件的第8行下面插入“哈哈哈哈”

[root@server ~]# sed -e '5ihello world' -e '8a哈哈哈哈哈' 1.txt  -e '5=;8='

sed -n '1,5p' 1.txt
sed -ne '1p' -ne '5p' 1.txt
sed -ne '1p;5p' 1.txt

過濾vsftpd.conf文件中以#開頭和空行:
[root@server ~]# grep -Ev '^#|^$' /etc/vsftpd/vsftpd.conf
[root@server ~]# sed -e '/^#/d' -e '/^$/d' /etc/vsftpd/vsftpd.conf
[root@server ~]# sed '/^#/d;/^$/d' /etc/vsftpd/vsftpd.conf
[root@server ~]# sed -r '/^#|^$/d' /etc/vsftpd/vsftpd.conf

過濾smb.conf文件中生效的行:
# sed -e '/^#/d' -e '/^;/d' -e '/^$/d' -e '/^\t$/d' -e '/^\t#/d' smb.conf
# sed -r '/^(#|$|;|\t#|\t$)/d' smb.conf 

# sed -e '/^#/d' -e '/^;/d' -e '/^$/d' -e '/^\t$/d' -e '/^\t#/' smb.conf

[root@server ~]# grep '^[^a-z]' 1.txt

[root@server ~]# sed -n '/^[^a-z]/p' 1.txt

過濾出文件中的IP地址:
[root@server ~]# grep -E '([0-9]{1,3}\.){3}[0-9]{1,3}' 1.txt 
192.168.0.254
[root@server ~]# sed -nr '/([0-9]{1,3}\.){3}[0-9]{1,3}/p' 1.txt 
192.168.0.254

[root@server ~]# grep -o -E '([0-9]{1,3}\.){3}[0-9]{1,3}' 2.txt 
10.1.1.1
10.1.1.255
255.255.255.0

[root@server ~]# sed -nr '/([0-9]{1,3}\.){3}[0-9]{1,3}/p' 2.txt
10.1.1.1
10.1.1.255
255.255.255.0
過濾出ifcfg-eth0文件中的IP、子網(wǎng)掩碼、廣播地址
[root@server shell06]# grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' ifcfg-eth0 
10.1.1.1
255.255.255.0
10.1.1.254
[root@server shell06]# sed -nr '/([0-9]{1,3}\.){3}[0-9]{1,3}/p' ifcfg-eth0|cut -d'=' -f2
10.1.1.1
255.255.255.0
10.1.1.254
[root@server shell06]# sed -nr '/([0-9]{1,3}\.){3}[0-9]{1,3}/p' ifcfg-eth0|sed -n 's/[A-Z=]//gp'
10.1.1.1
255.255.255.0
10.1.1.254

[root@server shell06]# ifconfig eth0|sed -n '2p'|sed -n 's/[:a-Z]//gp'|sed -n 's/ /\n/gp'|sed '/^$/d'
10.1.1.1
10.1.1.255
255.255.255.0
[root@server shell06]# ifconfig | sed -nr '/([0-9]{1,3}\.)[0-9]{1,3}/p' | head -1|sed -r 's/([a-z:]|[A-Z/t])//g'|sed 's/ /\n/g'|sed  '/^$/d'

[root@server shell06]# ifconfig eth0|sed -n '2p'|sed -n 's/.*addr:\(.*\) Bcast:\(.*\) Mask:\(.*\)/\1\n\2\n\3/p'
10.1.1.1 
10.1.1.255 
255.255.255.0

-i 選項  直接修改原文件
# sed -i 's/root/ROOT/;s/stu/STU/' 11.txt
# sed -i '17{s/YUNWEI/yunwei/;s#/bin/bash#/sbin/nologin#}' 1.txt
# sed -i '1,5s/^/#&/' a.txt
注意:
-ni  不要一起使用
p命令 不要再使用-i時使用
⑤ ==sed結(jié)合正則使用==

sed  選項  'sed命令或者正則表達式或者地址定位'  文件名

  1. 定址用于決定對哪些行進行編輯。地址的形式可以是數(shù)字、正則表達式、或二者的結(jié)合。
  2. 如果沒有指定地址,sed將處理輸入文件的所有行。
正則說明備注
/key/查詢包含關(guān)鍵字的行sed -n '/root/p' 1.txt
/key1/,/key2/匹配包含兩個關(guān)鍵字之間的行sed -n '/\^adm/,/^mysql/p' 1.txt
/key/,x從匹配關(guān)鍵字的行開始到文件第x行之間的行(包含關(guān)鍵字所在行)sed -n '/^ftp/,7p'
x,/key/從文件的第x行開始到與關(guān)鍵字的匹配行之間的行
x,y!不包含x到y(tǒng)行
/key/!不包括關(guān)鍵字的行sed -n '/bash$/!p' 1.txt

2. 腳本格式

1)用法

# sed -f scripts.sh  file       //使用腳本處理文件
建議使用   ./sed.sh   file

腳本的第一行寫上
#!/bin/sed -f
1,5d
s/root/hello/g
3i777
5i888
a999
p

2)注意事項

1) 腳本文件是一個sed的命令行清單。'commands'
2) 在每行的末尾不能有任何空格、制表符(tab)或其它文本。
3) 如果在一行中有多個命令,應該用分號分隔。
4) 不需要且不可用引號保護命令
5) #號開頭的行為注釋

3)舉例說明

# cat passwd
stu3:x:509:512::/home/user3:/bin/bash
stu4:x:510:513::/home/user4:/bin/bash
stu5:x:511:514::/home/user5:/bin/bash

# cat sed.sh 
#!/bin/sed -f
2a\
******************
2,$s/stu/user/
$a\
we inster new line
s/^[a-z].*/#&/

[root@server ~]# cat 1.sed 
#!/bin/sed -f
3a**********************
$chelloworld
1,3s/^/#&/

[root@server ~]# sed -f 1.sed -i 11.txt 
[root@server ~]# cat 11.txt 
#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
helloworld

3. 補充擴展總結(jié)

1、正則表達式必須以”/“前后規(guī)范間隔
例如:sed '/root/d' file
例如:sed '/^root/d' file

2、如果匹配的是擴展正則表達式,需要使用-r選來擴展sed
grep -E
sed -r
+ ? () {n,m} | \d

注意:         
在正則表達式中如果出現(xiàn)特殊字符(^$.*/[]),需要以前導 "\" 號做轉(zhuǎn)義
eg:sed '/\$foo/p' file

3、逗號分隔符
例如:sed '5,7d' file                  刪除5到7行
例如:sed '/root/,/ftp/d' file 
刪除第一個匹配字符串"root"到第一個匹配字符串"ftp"的所有行本行不找 循環(huán)執(zhí)行

4、組合方式
例如:sed '1,/foo/d' file          刪除第一行到第一個匹配字符串"foo"的所有行
例如:sed '/foo/,+4d' file         刪除從匹配字符串”foo“開始到其后四行為止的行
例如:sed '/foo/,~3d' file         刪除從匹配字符串”foo“開始刪除到3的倍數(shù)行(文件中)
例如:sed '1~5d' file              從第一行開始刪每五行刪除一行
例如:sed -nr '/foo|bar/p' file    顯示配置字符串"foo"或"bar"的行
例如:sed -n '/foo/,/bar/p' file   顯示匹配從foo到bar的行
例如:sed '1~2d'  file             刪除奇數(shù)行
例如:sed '0-2d'   file                刪除偶數(shù)行 sed '1~2!d'  file

5、特殊情況
例如:sed '$d' file                    刪除最后一行
例如:sed '1d' file                    刪除第一行

6、其他:
sed 's/.//' a.txt                       刪除每一行中的第一個字符
sed 's/.//2' a.txt                  刪除每一行中的第二個字符
sed 's/.//N' a.txt                  從文件中第N行開始,刪除每行中第N個字符(N>2)
sed 's/.$//' a.txt                  刪除每一行中的最后一個字符

[root@server ~]# cat 2.txt 
1 a
2 b
3 c
4 d
5 e
6 f
7 u
8 k
9 o
[root@server ~]# sed '/c/,~2d' 2.txt 
1 a
2 b
5 e
6 f
7 u
8 k
9 o


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI