一、简介
sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”,接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容发送往屏幕。然后读入下行,执行下一个循环。如果没有诸如“D”的特殊命令,那会在两个循环之间清空模式空间,但不能清除保留空间。这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出或 -i 。
选项
-n 不打印模式空间
-e 执行脚本、表达式来处理
-f 执行动作从文件读取执行
-i 修改原文件
-r 使用扩展正则表达式
命令
- s/regexp/replace/ 替换字符串(regexp为自己定义的值)
- p 打印当前模式空间
- P(大写) 打印模式空间的第一行
- d 删除模式空间,开始下一个循环
- D 删除模式空间的第一行,开始下一个循环
- = 打印当前行号
- a \text 当前行追加文本
- i \text 当前行上面插入文本
- c \text 所选行替换新文本
- q 立即退出sed脚本
- r 追加文本来自文件
- h H 复制/追加模式空间到保持空间
- g G 复制/追加保持空间到模式空间
- x 交换模式空间和保持空间内容
- l 打印模式空间的行,并显示控制字符$
- n N 读取/追加下一行输入到模式空间
- w filename 写入到当前模式空间到文件
- ! 取反、否定
- & 引用已匹配字符串
地址
first~step 从first开始,每step步输出值
$ 匹配最后一行
/regexp/ 正则表达式匹配行
number 只匹配指定行
addr1,addr2 开始匹配addr1行开始,直接addr2行结束
addr1,+N 从addr1行开始,向后的N行
addr1,~N 从addr1行开始,到N行结束
二、案例
案例1:
[root@rhel8 ~]# cat /tmp/services
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
匹配打印(p)
(1)打印匹配blp5开头的行
[root@rhel8 ~]#
[root@rhel8 ~]# cat /tmp/services | sed -n '/^blp5/p'
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
(2)打印第一行
[root@rhel8 ~]# cat /tmp/services | sed -n '1p'
nimgtw 48003/udp # Nimbus Gateway
(3)打印第一行至第三行
[root@rhel8 ~]# cat /tmp/services | sed -n '1,3p'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
(4)打印奇数行
[root@rhel8 ~]# seq 10 | sed -n '1~2p'
1
3
5
7
9
(5)打印匹配行及后一行
[root@rhel8 ~]# cat /tmp/services | sed -n '/blp5/,+1p'
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
(6)打印最后一行
[root@rhel8 ~]# cat /tmp/services | sed -n '$p'
iqobject 48619/udp # iqobject
(7)不打印最后一行
[root@rhel8 ~]# cat /tmp/services | sed -n '$!p'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
(8)匹配范围
[root@rhel8 ~]# cat /tmp/services | sed -n '/^blp5/,/^com/p'
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
匹配blp5所在行到最后一行
[root@rhel8 ~]# cat /tmp/services | sed -n '/blp5/,$p'
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
(9)引用系统变量,用引号
[root@rhel8 ~]# cat /tmp/services |sed -n ''$a',3p'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
或者
[root@rhel8 ~]# cat /tmp/services |sed -n "$a,3p"
案例2:删除匹配
[root@rhel8 ~]# cat /tmp/services | sed '/blp5/d'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
[root@rhel8 ~]# cat /tmp/services | sed '1d'
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
[root@rhel8 ~]# cat /tmp/services | sed '1~2d' 删除奇数行
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/udp # iqobject
[root@rhel8 ~]# cat /tmp/services | sed '1,3d'
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
打印时把匹配的打印出来,删除时把匹配的删除,删除只是不用-n选项
练习:去除空格/etc/httpd/conf/httpd.conf文件的空行或开头#号的行
sed '/^#/d;/^$/d' /etc/httpd/conf/httpd.conf
案例3:替换(s///)
(1)替换blp5字符串为test
[root@rhel8 ~]# cat /tmp/services | sed 's/blp5/test/'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
test 48129/tcp # Bloomberg locator
test 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
全局替换加g
[root@rhel8 ~]# cat /tmp/services | sed 's/blp5/test/g'
(2)替换开头是blp5的字符串并打印
[root@rhel8 ~]# cat /tmp/services | sed -n 's/^blp5/test/p'
test 48129/tcp # Bloomberg locator
test 48129/udp # Bloomberg locator
(3)使用&命令引用匹配内容并替换
[root@rhel8 ~]# cat /tmp/services | sed 's/48049/&.123/'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049.123/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
ip加引号
[root@rhel8 ~]# echo '10.10.10.1 10.10.10.2 10.10.10.3' | sed -r 's/[^ ]+/"&"/g'
"10.10.10.1" "10.10.10.2" "10.10.10.3"
(4)对1-5行的blp5进行替换
[root@rhel8 ~]# cat /tmp/services | sed '1,5s/blp5/test/'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
test 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
(5)对匹配行进行替换,先匹配出来48129/tcp所在行,然后再将改行的blp5字符串替换为test
[root@rhel8 ~]# cat /tmp/services | sed '/48129\/tcp/s/blp5/test/'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
test 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
(6)二次匹配替换
[root@rhel8 ~]# cat /tmp/services | sed 's/blp5/test/;s/3g/4g/'
nimgtw 48003/udp # Nimbus Gateway
4gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
test 48129/tcp # Bloomberg locator
test 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
(7)分组使用
[root@rhel8 ~]# cat /tmp/services | sed -r 's/(.*)(480.*)(#.*)/\1\2test \3/'
nimgtw 48003/udp test # Nimbus Gateway
3gpp-cbsp 48049/tcp test # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
(8)将协议与端口号位置互换
[root@rhel8 ~]# cat /tmp/services | sed -r 's/(.*)(\<[0-9]+\>)\/(tcp|udp)(.*)/\1\3\/\2\4/'
nimgtw udp/48003 # Nimbus Gateway
3gpp-cbsp tcp/48049 # 3GPP Cell Broadcast Service Protocol
isnetserv tcp/48128 # Image Systems Network Services
isnetserv udp/48128 # Image Systems Network Services
blp5 tcp/48129 # Bloomberg locator
blp5 udp/48129 # Bloomberg locator
com-bardac-dw tcp/48556 # com-bardac-dw
com-bardac-dw udp/48556 # com-bardac-dw
iqobject tcp/48619 # iqobject
iqobject udp/48619 # iqobject
(9)位置调换
[root@rhel8 ~]# echo "abc cde xyz" | sed -r 's/(.*)x/\1X/'
abc cde Xyz
[root@rhel8 ~]# echo "abc cde xxyxz" | sed -r 's/(.*)x/\1X/'
abc cde xxyXz
[root@rhel8 ~]# echo "abc cde xxyxz" | sed -r 's/(.*)y/\1Y/'
abc cde xxYxz
将cde和456互换位置
[root@rhel8 ~]# echo "abc:cde;123:456" | sed -r 's/([^:]*)(;.*:)([^:]+$)/\3\2\1/'
abc:456;123:cde
(10)注释匹配行后的多少行
[root@rhel8 ~]# seq 10 |sed '/5/,+3s/^/#/'
1
2
3
4
#5
#6
#7
#8
9
10
(11)注释指定多行
[root@rhel8 ~]# seq 5 |sed -r '/^3|^4/s/^/#/'
1
2
#3
#4
5
[root@rhel8 ~]# seq 5 |sed -r 's/^3|^4/#&/'
1
2
#3
#4
5
案例4:多重编辑
删除1-4行,并将剩余行的blp5替换为test
[root@rhel8 ~]# cat /tmp/services | sed -e '1,4d' -e 's/blp5/test/'
test 48129/tcp # Bloomberg locator
test 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
或
[root@rhel8 ~]# cat /tmp/services |sed '1,4d;s/blp5/test/'
test 48129/tcp # Bloomberg locator
test 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
案例5:添加新内容(a、i、c)
(1)在blp5上一行添加test------------i
[root@rhel8 ~]# cat /tmp/services |sed '/blp5/i \test'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
test
blp5 48129/tcp # Bloomberg locator
test
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
[root@rhel8 ~]#
(2)在blp5下一行添加test-----------------a
[root@rhel8 ~]# cat /tmp/services |sed '/blp5/a \test'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
test
blp5 48129/udp # Bloomberg locator
test
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
(3)将blp5所在行替换为新行test-------------------c
[root@rhel8 ~]# cat /tmp/services |sed '/blp5/c \test'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
test
test
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
(4)在指定行的下一行添加一行
[root@rhel8 ~]# cat /tmp/services |sed '2a \test'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
test
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
(5)在指定行前面和后面添加一行
[root@rhel8 ~]# seq 5 |sed '3s/.*/txt\n&/'
1
2
txt
3
4
5
[root@rhel8 ~]# seq 5 |sed '3s/.*/&\ntest/'
1
2
3
test
4
5
案例6:读取文件并追加到匹配行后(r)
[root@rhel8 ~]# cat a.txt
123
456
[root@rhel8 ~]# cat /tmp/services | sed '/blp5/r a.txt'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
123
456
blp5 48129/udp # Bloomberg locator
123
456
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
案例7: 将匹配行写到文件(w)
[root@rhel8 ~]# cat /tmp/services | sed '/blp5/w b.txt'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
[root@rhel8 ~]# cat b.txt
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
案例8:读取下一行(n和N)
n 读取下一行到模式空间。
N 追加下一行内容到模式空间,并以换行符\n 分隔。
(1)打印匹配的下一行
[root@rhel8 ~]# seq 5 |sed -n '/3/{n;p}'
4
(2)打印偶数
[root@rhel8 ~]# seq 6 |sed -n 'n;p'
2
4
6
sed 先读取第一行 1,执行 n 命令,获取下一行 2,此时模式空间是 2,执行 p 命令,打印模式空
间。 现在模式空间是 2,sed 再读取 3,执行 n 命令,获取下一行 4,此时模式空间为 4,执行 p 命
令,以此类推。
(3)打印奇数
[root@rhel8 ~]# seq 6 |sed 'n;d'
1
3
5
读取1,执行n,得出2,执行d,删除2,得空,以此类推,读取3,执行n,得出4,
执行d,删除4,得空,但是读取5时,得出6,执行d,删除6。
因无-n参数,故输出1\n3\n5
[root@rhel8 ~]# seq 6 |sed -n 'p:n'
1
3
5
(4)每三行执行一次p命令
[root@rhel8 ~]# seq 6 |sed 'n;n;p'
1
2
3
3
4
5
6
6
sed 先读取第一行 1,并打印模式空间 1,执行 n 命令,获取下一行 2,并打印模式空间 2,再执行 n
命令,获取下一行 3,执行 p 命令,打印模式空间 3。sed 读取下一行 3,并打印模式空间 3,以此类
推。
(5)每三行替换一次
方法1:只是把p命令改成了替换命令
[root@rhel8 ~]# seq 6 |sed 'n;n;s/^/=/;s/$/=/'
1
2
=3=
4
5
=6=
方法2:这次用到了地址匹配,来实现上面的效果
[root@rhel8 ~]# seq 6 |sed '3~3{s/^/=/;s/$/=/}'
1
2
=3=
4
5
=6=
当执行多个 sed 命令时,有时相互会产生影响,我们可以用大括号{}把他们括起来
(6)再看下N命令的功能
[root@rhel8 ~]# seq 6 |sed 'N;q'
1
2
将两行合并一行:
[root@rhel8 ~]# seq 6 |sed 'N;s/\n//'
12
34
56
第一个命令:sed 读取第一行 1,N 命令读取下一行 2,并以\n2 追加,此时模式空间是 1\n2,再执
行 q 退出。
为了进一步说明 N 的功能,看第二个命令:执行 N 命令后,此时模式空间是 1\n2,再执行把\n 替换
为空,此时模式空间是 12,并打印。
[root@rhel8 ~]# seq 5 |sed -n 'N;p'
1
2
3
4
[root@rhel8 ~]# seq 6 |sed -n 'N;p'
1
2
3
4
5
6
为什么第一个不打印 5 呢?
因为 N 命令是读取下一行追加到 sed 读取的当前行,当 N 读取下一行没有内容时,则退出,也不会
执行 p 命令打印当前行。
当行数为偶数时,N 始终就能读到下一行,所以也会执行 p 命令。
[root@rhel8 ~]# seq 5 |sed -n '$!N;p'
1
2
3
4
5
加一个满足条件,当 sed 执行到最后一行时,用感叹号不去执行 N 命令,随后执行 p 命令
案例9:打印和删除模式空间第一行(P(大写)和D)
P(大写) 打印模式空间的第一行。
D 删除模式空间的第一行。
打印奇数
[root@rhel8 ~]# seq 6 |sed -n 'N;P'
1
3
5
[root@rhel8 ~]# seq 6 |sed 'N;D'
6
读取第一行 1,执行 N 命令读取下一行并追加到模式空间,此时模式空间是 1\n2,执行 D 命令删除
模式空间第一行 1,剩余 2。
读取1,执行N,得出1\n2,执行D,得出2,执行N,得出2\n3,执行D,得出3,依此类推,得出6,
执行N,条件失败退出,因无-n参数,故输出6
案例10:保持空间操作(h与H,g与G和x)
- h 复制模式空间内容到保持空间(覆盖)。
- H 复制模式空间内容追加到保持空间。
- g 复制保持空间内容到模式空间(覆盖)。
- G 复制保持空间内容追加到模式空间。
- x 模式空间与保持空间内容互换。
(1)将匹配的内容覆盖到另一个匹配
[root@rhel8 ~]#seq 6 |sed -e '/3/{h;d}' -e '/5/g'
1
2
4
3
6
h 命令把匹配的 3 复制到保持空间,d 命令删除模式空间的 3。后面命令再对模式空间匹配 5,并用
g 命令把保持空间 3 覆盖模式空间 5。
(2)将匹配的内容放到最后
[root@rhel8 ~]# seq 6 |sed -e '/3/{h;d}' -e '$G'
1
2
4
5
6
3
(3)交换模式空间和保持空间
[root@rhel8 ~]#seq 6 |sed -e '/3/{h;d}' -e '/5/x' -e '$G'
1
2
4
3
6
5
在模式空间匹配 5 并将保持空间的 3 与 5 交换,5 就变成了3,最后把保持空间的 5 追加到模式空间的。