一、sed編輯器簡介
sed是一種流編輯器,流編輯器會在編輯器處理數(shù)據(jù)前基于預(yù)先提供的一組規(guī)則來編輯數(shù)據(jù)。
sed編輯器可以根據(jù)命令來處理數(shù)據(jù)流中的數(shù)據(jù),通過多種轉(zhuǎn)換修改流經(jīng)它的文本,這些命令要么從命令行中輸入,要么存儲在一個(gè)命令文本文件中。
sed是一個(gè)面向字符流的非交互式編輯器,也就是說 sed 不允許用戶與它進(jìn)行交互操作。
二、sed工作流程
1.讀取
sed從輸入流(文件、管道、標(biāo)準(zhǔn)輸入)中讀取一行內(nèi)容并存儲到臨時(shí)的內(nèi)存緩沖區(qū)中(又稱模式空間,pattern space)。
2.執(zhí)行
默認(rèn)情況下,所有的sed命令都在模式空間中順序地執(zhí)行,除非指定了行的地址,否則sed命令將會在所有的行上依次執(zhí)行。
3.顯示
發(fā)送修改后的內(nèi)容到輸出流。在發(fā)送數(shù)據(jù)后,模式空間將會被清空。在所有的文件內(nèi)容都被處理完成之前,上述過程將重復(fù)執(zhí)行,直至所有內(nèi)容被處理完。
注:默認(rèn)情況下所有的sed命令都是在模式空間內(nèi)執(zhí)行的,不會影響到文件中的內(nèi)容,除非是用重定向輸出到文件或使用-i選項(xiàng)。
三、sed命令
sed [選項(xiàng)] 操作 文件名…
常用選項(xiàng)
常用操作
執(zhí)行多個(gè)操作的方法
- sed -e ‘操作1’ -e ‘操作2’ … 文件名
- sed -e ‘操作1;操作2;…’ 文件名
四、sed命令的使用
1.sed打印文件內(nèi)容(p)
(1)打印文件所有行
sed本身有執(zhí)行操作后自動打印的效果,-n禁用,p操作也可以打印,并且可以限定條件
sed ’ ’ 文件名
sed -n ‘p’ 文件名
//使用sed自動打印
[root@localhost1 sedtest]#sed '' file1
one
two
three
four
five
six
seven
eight
nine
ten
//使用sed的p操作打印
[root@localhost1 sedtest]#sed -n 'p' file1
one
two
three
four
five
six
seven
eight
nine
ten
(2)打印文件指定行
sed -n ‘指定條件p’ 文件名
sed -n ‘/字符串或正則表達(dá)式/p’ 文件名
//1.打印指定行(根據(jù)行號)
[root@localhost1 sedtest]#sed -n '2p' file1 //打印第2行
two
//2.打印指定的多行(根據(jù)行號)
[root@localhost1 sedtest]#sed -n '7,$p' file1 //打印從第7行到末尾行
seven
eight
nine
ten
//3.打印多行后退出
[root@localhost1 sedtest]#sed '3q' file1 //從頭打印3行后退出
one
two
three
//4.打印指定行(根據(jù)字符串)
[root@localhost1 sedtest]#sed -n '/ve/p' file1 //根據(jù)字符串s
five
seven
//5.從指定行打印到包含指定字符串的行(根據(jù)字符串)
[root@localhost1 sedtest]#sed -n '7,/nobody/p' /etc/passwd //打印7到包含nobody的行
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
//6.打印奇偶行
[root@localhost1 sedtest]#sed -n 'n;p' file1 //n表示跳到下一行,先n實(shí)現(xiàn)輸出偶數(shù)行
two
four
six
eight
ten
[root@localhost1 sedtest]#sed -n 'p;n' file1 //后n實(shí)現(xiàn)奇數(shù)行
one
three
five
seven
nine
//7.正則表達(dá)式表示
//根據(jù)root開頭或包含mail的輸出行(包含擴(kuò)展正則時(shí)sed一定需要加-r選項(xiàng))
[root@localhost1 sedtest]#sed -nr '/^root|mail/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
//從第8行打印到包含(r 至少有一個(gè)a t)的字符串的行
[root@localhost1 sedtest]#sed -nr '8,/ra{1,}t/p' /etc/passwd
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
2.sed增加、插入、替換行(a、i、c)
(1)a操作在指定行下增加行內(nèi)容
sed ‘指定行范圍a 指定內(nèi)容’ 文件名
//指定第2行下增加行內(nèi)容
[root@localhost1 ~]#sed '2a ABC' file1
One Two Three
Four Five Six
ABC
Seven Eight Nine
Ten Eleven Twelve
//指定2到4行每行下增加行內(nèi)容
[root@localhost1 ~]#sed '2,4a ABC' file1
One Two Three
Four Five Six
ABC
Seven Eight Nine
ABC
Ten Eleven Twelve
ABC
(2)i操作在指定行上插入行內(nèi)容
sed ‘指定行范圍i 指定內(nèi)容’ 文件名
//指定第2行上插入行內(nèi)容
[root@localhost1 ~]#sed '2i ABC' file1
One Two Three
ABC
Four Five Six
Seven Eight Nine
Ten Eleven Twelve
//指定第1到3行上插入行內(nèi)容
[root@localhost1 ~]#sed '1,3i ABC' file1
ABC
One Two Three
ABC
Four Five Six
ABC
Seven Eight Nine
Ten Eleven Twelve
(3)c操作指定行替換為一行內(nèi)容
sed ‘指定行范圍c 指定內(nèi)容’ 文件名
//指定第3行替換為指定行內(nèi)容
[root@localhost1 ~]#sed '3c ABC' file1
One Two Three
Four Five Six
ABC
Ten Eleven Twelve
//指定第3到4行替換為指定行內(nèi)容(多行變?yōu)橐恍校?[root@localhost1 ~]#sed '3,4c ABC' file1
One Two Three
Four Five Six
ABC
//指定包含ve的行替換為指定行內(nèi)容
[root@localhost1 ~]#sed '/ve/c ABC' file1
One Two Three
ABC
ABC
ABC
3.sed刪除文件內(nèi)容(d)
sed ‘指定條件d’ 文件名
sed ‘/字符串或正則表達(dá)式/d’ 文件名
//1.刪除所有行(根據(jù)行號)
[root@localhost1 sedtest]#sed 'd' file1
//2.刪除指定行(根據(jù)行號)
[root@localhost1 sedtest]#sed '1d' file1 //刪除第1行
two
three
four
five
six
seven
eight
nine
ten
//3.刪除指定的多行(根據(jù)行號)
[root@localhost1 sedtest]#sed '2,8d' file1 //刪除2到8行
one
nine
ten
//4.刪除空行
[root@localhost1 sedtest]#sed '/^$/d' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Jul 29 21:42:19 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=552d20a1-63f1-4209-af23-7bcdb31d1c84 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
//5.刪除指定的多行(根據(jù)字符串)
[root@localhost1 sedtest]#sed '/ve/d' file1 //刪除包含ve的行
one
two
three
four
six
eight
nine
ten
//6.正則表達(dá)式表示
//刪除結(jié)尾為bash或nologin的行
[root@localhost1 sedtest]#sed -r '/bash$|nologin$/d' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
//7.從包含第1個(gè)字符串的行開始刪除,到包含第2個(gè)字符串的位置停止刪除
//(如果找不到第2個(gè)字符串的行,則刪到最后一行)
[root@localhost1 sedtest]#sed '/one/,/five/d' file1 //從含one的行刪到含five的行
six
seven
eight
nine
ten
[root@localhost1 sedtest]#sed '/thr/,/no/d' file1 //從含thr的行開始刪,匹配不到含no的行,則刪到最后
one
two
//8.反向刪除(刪除除了包含指定內(nèi)容的行)
[root@localhost1 sedtest]#sed '/ve/d' file1 //刪除除包含ve的行
five
seven
4.sed替換文件內(nèi)容(s)
sed [選項(xiàng)] ‘行范圍 s/舊字符串/新字符串/替換標(biāo)記’ 文件名
4種替換標(biāo)記
//1.不加范圍,替換每行查找到的字符串
[root@localhost1 ~]#sed -n 's/root/admin/p' /etc/passwd
admin:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/admin:/sbin/nologin
//2.指定范圍行,在指定行中替換n次(默認(rèn)1次)
[root@localhost1 ~]#sed -n '/^root/ s/root/abc/p' /etc/passwd
abc:x:0:0:root:/root:/bin/bash
[root@localhost1 ~]#sed -n '/^root/ s/root/abc/2p' /etc/passwd
root:x:0:0:abc:/root:/bin/bash
//3.替換行中所有,使用g
[root@localhost1 ~]#sed -n '/^root/ s/root/abc/gp' /etc/passwd
root:x:0:0:root:/abc:/bin/bash
//4.指定行s/^/#/可以實(shí)現(xiàn)注釋行內(nèi)容
[root@localhost1 ~]#sed '2 s/^/#/' file1
one two three
#four five six
seven eight nine
ten eleven twelve
//5.指定行范圍替換
[root@localhost1 ~]#sed '1,3 s/^/#/' file1
#one two three
#four five six
#seven eight nine
ten eleven twelve
//指定替換除為空可以實(shí)現(xiàn)將指定字符串刪除
[root@localhost1 ~]#sed 's/#//' /etc/fstab
/etc/fstab
Created by anaconda on Fri Jul 29 21:42:19 2022
Accessible filesystems, by reference, are maintained under '/dev/disk'
See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
/dev/mapper/centos-root / xfs defaults 0 0
UUID=552d20a1-63f1-4209-af23-7bcdb31d1c84 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
5.sed使用命令文件執(zhí)行(-f)
//編寫文件包含sed的操作
[root@localhost1 ~]#vim script.txt
/^$/d
/e$/ s/^/#/
1,3 s/#//
//測試用文件
[root@localhost1 ~]#vim file2
#one
#two
#three
four
five
six
seven
eight
nine
ten
//sed指定文件操作
[root@localhost1 ~]#sed -f script.txt file2
#one
two
#three
four
five
six
seven
eight
#nine
ten
6.sed直接修改文件內(nèi)容(-i)
//加-i直接對文件進(jìn)行操作
[root@localhost1 ~]#sed -i '/swap/ s/^/#/' /etc/fstab
//注釋文件中包含swap的行
[root@localhost1 ~]#vim /etc/fstab
# /etc/fstab
# Created by anaconda on Fri Jul 29 21:42:19 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=552d20a1-63f1-4209-af23-7bcdb31d1c84 /boot xfs defaults 0 0
#/dev/mapper/centos-swap swap swap defaults 0 0
7.sed擴(kuò)充操作
(1)替換處&代表需要替換內(nèi)容處中正則表達(dá)式的內(nèi)容
sed [選項(xiàng)] ‘行范圍 s/正則表達(dá)式/&/替換標(biāo)記’ 文件名
//這里的&代表.*swap.*匹配到的內(nèi)容(即包含swap的行內(nèi)容),在前面加上#可以實(shí)現(xiàn)注釋行
[root@localhost1 ~]#sed -n 's/.*swap.*/#&/' /etc/fstab
#/dev/mapper/centos-swap swap swap defaults 0 0
(2)替換處\1…\n\ 對應(yīng)需要替換內(nèi)容處每個(gè)括號中子表達(dá)式(可以是字符串和正則)
sed [選項(xiàng)] ‘s/(表達(dá)式1)…(表達(dá)式n)/\1\2…\n/’ 文件名
//原文件
[root@localhost1 ~]#cat file3
123456abcdef
//字符串子表達(dá)示,"\數(shù)字" 一一對應(yīng)
[root@localhost1 ~]#sed 's/(123)(456)(abc)/\3\2\1/' file3
abc456123def
//正則子表達(dá)示,"\數(shù)字" 一一對應(yīng)
[root@localhost1 ~]#sed -r 's/([0-9]{6})([a-z]{6})/\2\1/' file3
abcdef123456
(3)大小寫轉(zhuǎn)換
sed ‘s/[a-z]/\u&/g’ 文件名 (原字符范圍前\b實(shí)現(xiàn)首字母轉(zhuǎn)大寫)
sed ‘s/[A-Z]/\l&/g’ 文件名
//原文件
[root@localhost1 ~]#cat file1
one two three
four five six
seven eight nine
ten eleven twelve
//1.全部轉(zhuǎn)為大寫
[root@localhost1 ~]#sed 's/[a-z]/\u&/g' file1
ONE TWO THREE
FOUR FIVE SIX
SEVEN EIGHT NINE
TEN ELEVEN TWELVE
[root@localhost1 ~]#sed -i 's/[a-z]/\u&/g' file1
//2.全部轉(zhuǎn)為小寫
[root@localhost1 ~]#sed 's/[A-Z]/\l&/g' file1
one two three
four five six
seven eight nine
ten eleven twelve
//3.錨定首字母,實(shí)現(xiàn)首字母大小寫轉(zhuǎn)換
[root@localhost1 ~]#sed 's/\b[a-z]/\u&/g' file1
One Two Three
Four Five Six
Seven Eight Nine
Ten Eleven Twelve
/U/對應(yīng)子表達(dá)式位置數(shù)/E (結(jié)合第2條操作實(shí)現(xiàn)部分轉(zhuǎn)大小寫)
//部分轉(zhuǎn)大小寫,實(shí)現(xiàn)修改文件前綴名小寫轉(zhuǎn)大寫
[root@localhost1 ~]#ls *.txt | sed -r 's/(.+)(\.txt)/\u\1\E\2/' //u轉(zhuǎn)開頭
Abc.txt
A.txt
B.txt
Efg.txt
Number.txt
Script.txt
[root@localhost1 ~]#ls *.txt | sed -r 's/(.+)(\.txt)/\U\1\E\2/' //U轉(zhuǎn)全部
ABC.txt
A.txt
B.txt
EFG.txt
NUMBER.txt
SCRIPT.txt
(4)將多個(gè)文件內(nèi)容放在一個(gè)文件中
sed -i ‘$r 文件1’ 文件2文章來源:http://www.zghlxwxcb.cn/news/detail-477693.html
//往fa、fb文件輸入內(nèi)容
[root@localhost1 ~]#echo 12345678 > fa
[root@localhost1 ~]#echo abcdefgh > fb
//合并文件內(nèi)容到fb文件中
[root@localhost1 ~]#sed -i '$r fa' fb
[root@localhost1 ~]#cat fb
abcdefgh
12345678
(5)復(fù)制、剪切粘貼行內(nèi)容
H 復(fù)制 ; G 粘貼文章來源地址http://www.zghlxwxcb.cn/news/detail-477693.html
//原文件
[root@localhost1 ~]#cat file2
zhang san
li si
wang wu
zhao liu
qian qi
zhu ba
//1.復(fù)制粘貼到末尾
[root@localhost1 ~]#sed '1,2H;$G' file2
zhang san
li si
wang wu
zhao liu
qian qi
zhu ba
zhang san
li si
//2.剪切粘貼到末尾
[root@localhost1 ~]#sed '1,2{H;d};$G' file2
wang wu
zhao liu
qian qi
zhu ba
zhang san
li si
到了這里,關(guān)于Shell腳本文本三劍客之sed編輯器(擁明月入懷,攬星河入夢)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!