shell正则表达式—— sed命令

sed概述

sed 是流编辑器(Stream Editor),在Shell 脚本和 Makefile 中作为过滤器使用。把一个程序的输入引入 sed 的出入,经过一些列的命令转换为另一种格式输出。

sed工作流程

sed 的工作流程主要包括读取、执行和显示三个过程。
 读取:sed 从输入流(文件、管道、标准输入)中读取一行内容并存储到临时的缓冲区中(又称模式空间,pattern space)。
 执行:默认情况下,所有的 sed 命令都在模式空间中顺序地执行,除非指定了行的地址,否则 sed 命令将会在所有的行上依次执行。
 显示:发送修改后的内容到输出流。在发送数据后,模式空间将会被清空。

sed使用

sed [选项] 操作 参数

常见选项

e 或–expression=:表示用指定命令或者脚本来处理输入的文本文件。
-f 或–file=:表示用指定的脚本文件来处理输入的文本文件。
-h 或–help:显示帮助。
-n、–quiet 或 silent:表示仅显示处理后的结果。
-i:直接编辑文本文件。

常见操作

a:增加,在当前行下面增加一行指定内容。
c:替换,将选定行替换为指定内容。
s:替换,替换指定字符。
d:删除,删除选定的行。
i:插入,在选定行上面插入一行指定内容。
p:打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容;如果有非打印字符,则以 ASCII 码输出。其通常与“-n”选项一起使用。
y:字符转换。

注:sed 除非加 -i 否则不会对源文件进行修改

输出

这条命令和cat wenben.txt 效果一样

[root@1centos zhengzebiaodashi]# sed -n 'p' wenben.txt 
s short and fat.
He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
The year ahead will test our political establishment to the limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words
1

wd
#woood
# #woooooood #
wwod
wwwwoooooood
AxyzxyzxyzxyzC

I bet this place is really spooky late at night! Misfortunes never come alone/single.
 
I shouldn't have lett so tast.

输出第三行

[root@1centos zhengzebiaodashi]# sed -n '3p' wenben.txt 
the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.

输出 3 到 5 行

[root@1centos zhengzebiaodashi]# sed -n '3,5p' wenben.txt 
the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
The year ahead will test our political establishment to the limit.
PI=3.141592653589793238462643383249901429

隔行输出

这边我再创一个 1 到 15 的txt文件好了
n 表示读入下一行资料

输出奇数行

[root@1centos zhengzebiaodashi]# sed -n 'p;n' shuzi.txt 
1
3
5
7
9
11
13
15

输出偶数行

[root@1centos zhengzebiaodashi]# sed -n 'n;p' shuzi.txt 
2
4
6
8
10
12
14

输出1-5行的奇数行

[root@1centos zhengzebiaodashi]# sed -n '1,5{p;n}' shuzi.txt 
1
3
5

输出1-5行的偶数行

可以发现第六行也被读出来了,可以理解成到第五行的时候执行了n,所以展示了第六行

[root@1centos zhengzebiaodashi]# sed -n '1,5{n;p}' shuzi.txt 
2
4
6

输出特定的的行

[root@1centos zhengzebiaodashi]# sed -n '/the/p' wenben.txt 
the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
The year ahead will test our political establishment to the limit.
[root@1centos zhengzebiaodashi]# 

输出 1 -4行中包含 the 的行

[root@1centos zhengzebiaodashi]# sed -n '1,4{/the/p}' wenben.txt 
the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
The year ahead will test our political establishment to the limit.
[root@1centos zhengzebiaodashi]# 

输出从三行开始到包含 the 的行为止

[root@1centos zhengzebiaodashi]# sed -n '1,/the/p' wenben.txt 
s short and fat.
He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.

输出包含 the 的所在行行号

[root@1centos zhengzebiaodashi]# sed -n '/the/=' wenben.txt 
3
4

输出以 PI 开头的行

[root@1centos zhengzebiaodashi]# sed -n '/^PI/p' wenben.txt 
PI=3.141592653589793238462643383249901429

输出以数字结尾的行

[root@1centos zhengzebiaodashi]# sed -n '/[0-9]$/p' wenben.txt 
PI=3.141592653589793238462643383249901429
1

输出包含 wood 的行

<、>代表单词边界,精确输出

[root@1centos zhengzebiaodashi]# sed -n '/\<wood\>/p' wenben.txt 
a wood cross!

删除

nl命令用于计算文件行数

基本上删整行

删除第 3 行

[root@1centos zhengzebiaodashi]# nl wenben.txt |sed '3d'
     1	s short and fat.
     2	He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
     4	The year ahead will test our political establishment to the limit.
     5	PI=3.141592653589793238462643383249901429
     6	a wood cross!
     7	Actions speak louder than words
     8	1
       
     9	wd
    10	#woood
    11	# #woooooood #
    12	wwod
    13	wwwwoooooood
    14	AxyzxyzxyzxyzC
       
    15	I bet this place is really spooky late at night! Misfortunes never come alone/single.
    16	 
    17	I shouldn't have lett so tast.

删除 3-5 行

[root@1centos zhengzebiaodashi]# nl wenben.txt |sed '3,5d'
     1	s short and fat.
     2	He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
     6	a wood cross!
     7	Actions speak louder than words
     8	1
       
     9	wd
    10	#woood
    11	# #woooooood #
    12	wwod
    13	wwwwoooooood
    14	AxyzxyzxyzxyzC
       
    15	I bet this place is really spooky late at night! Misfortunes never come alone/single.
    16	 
    17	I shouldn't have lett so tast.

仅保留包含 cross 的行

[root@1centos zhengzebiaodashi]# nl wenben.txt |sed '/cross/!d'
     6	a wood cross!

删除小写字母开头的行

[root@1centos zhengzebiaodashi]# sed '/^[a-z]/d' wenben.txt 
He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
The year ahead will test our political establishment to the limit.
PI=3.141592653589793238462643383249901429
Actions speak louder than words
1

#woood
# #woooooood #
AxyzxyzxyzxyzC

I bet this place is really spooky late at night! Misfortunes never come alone/single.
 
I shouldn't have lett so tast.

删除以 . 结尾的行

[root@1centos zhengzebiaodashi]# sed '/\.$/d' wenben.txt 
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words
1

wd
#woood
# #woooooood #
wwod
wwwwoooooood
AxyzxyzxyzxyzC

删除空行

注:若空行里有一个空格是删不掉的

[root@1centos zhengzebiaodashi]# sed '/^$/d' wenben.txt 
s short and fat.
He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12! google is the best tools for search keyword.
The year ahead will test our political establishment to the limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words
1
wd
#woood
# #woooooood #
wwod
wwwwoooooood
AxyzxyzxyzxyzC
I bet this place is really spooky late at night! Misfortunes never come alone/single.
I shouldn't have lett so tast.

替换

在使用 sed 命令进行替换操作时需要用到 s(字符串替换)、c(整行/整块替换)、y
(字符转换)命令选项

替换符合条件的文本

为方便观察,先创建一个文件

[root@1centos zhengzebiaodashi]# vim the.txt
1thethethe
2thethethe
3thethethethethe
4thethethethethethethe
5thethethethe
6hethethe
7thethethe
8thethethethethe
9thethethethethethethe
10thethethethe

将每行中第一个 the 替换成 THE

[root@1centos zhengzebiaodashi]# sed 's/the/THE/' the.txt 
1THEthethe
2THEthethe
3THEthethethethe
4THEthethethethethethe
5THEthethethe
6heTHEthe
7THEthethe
8THEthethethethe
9THEthethethethethethe
10THEthethethe

将 1-5 行的第一个 the 替换成 THE

[root@1centos zhengzebiaodashi]# sed '1,5s/the/THE/' the.txt 
1THEthethe
2THEthethe
3THEthethethethe
4THEthethethethethethe
5THEthethethe
6hethethe
7thethethe
8thethethethethe
9thethethethethethethe
10thethethethe

将每行中的第 2 个the替换成wasd

[root@1centos zhengzebiaodashi]# sed 's/the/wasd/2' the.txt 
1thewasdthe
2thewasdthe
3thewasdthethethe
4thewasdthethethethethe
5thewasdthethe
6hethewasd
7thewasdthe
8thewasdthethethe
9thewasdthethethethethe
10thewasdthethe

将文件中的所有the 替换为 THE

[root@1centos zhengzebiaodashi]# sed 's/the/well/g' the.txt 
1wellwellwell
2wellwellwell
3wellwellwellwellwell
4wellwellwellwellwellwellwell
5wellwellwellwell
6hewellwell
7wellwellwell
8wellwellwellwellwell
9wellwellwellwellwellwellwell
10wellwellwellwell

将文中所有 h 去掉

[root@1centos zhengzebiaodashi]# sed 's/h//g' the.txt 
1tetete
2tetete
3tetetetete
4tetetetetetete
5tetetete
6etete
7tetete
8tetetetete
9tetetetetetete
10tetetete
[root@1cen

将每行开头加上#

[root@1centos zhengzebiaodashi]# sed 's/^/#/' the.txt 
#1thethethe
#2thethethe
#3thethethethethe
#4thethethethethethethe
#5thethethethe
#6hethethe
#7thethethe
#8thethethethethe
#9thethethethethethethe
#10thethethethe

将包含数字的行结尾增加 +++

[root@1centos zhengzebiaodashi]# sed '/[0-9]/s/$/+++/g' the.txt 
1thethethe+++
2thethethe+++
3thethethethethe+++
4thethethethethethethe+++
5thethethethe+++
6hethethe+++
7thethethe+++
8thethethethethe+++
9thethethethethethethe+++
10thethethethe+++

将包含the 的所有行中的 数字 都替换为 A

[root@1centos zhengzebiaodashi]# sed '/the/s/[0-9]/A/g' the.txt 
Athethethe
Athethethe
Athethethethethe
Athethethethethethethe
Athethethethe
Ahethethe
Athethethe
Athethethethethe
Athethethethethethethe
AAthethethethe

迁移

相当于剪切黏贴
H:复制到剪贴板;
g、G:将剪贴板中的数据覆盖/追加至指定行;
w:保存为文件;
r:读取指定文件;
a:追加指定内容。

迁移符合条件的文本

将包含 2 的行迁移到文件末行

默认会出空行

[root@1centos zhengzebiaodashi]# sed '/2/{H;d};$G' the.txt 
1thethethe
3thethethethethe
4thethethethethethethe
5thethethethe
6hethethe
7thethethe
8thethethethethe
9thethethethethethethe
10thethethethe

2thethethe

但是可以删除空行

[root@1centos zhengzebiaodashi]# sed '/2/{H;d};$G' the.txt |sed '/^$/'d
1thethethe
3thethethethethe
4thethethethethethethe
5thethethethe
6hethethe
7thethethe
8thethethethethe
9thethethethethethethe
10thethethethe
2thethethe

将1-3 的行放在第 5 行后

[root@1centos zhengzebiaodashi]# sed '1,3{H;d};5G' the.txt 
4thethethethethethethe
5thethethethe

1thethethe
2thethethe
3thethethethethe
6hethethe
7thethethe
8thethethethethe
9thethethethethethethe
10thethethethe

将 包含 2 的行 放在文件 fg.txt中

[root@1centos zhengzebiaodashi]# sed '/2/w fg.txt' the.txt 
1thethethe
2thethethe
3thethethethethe
4thethethethethethethe
5thethethethe
6hethethe
7thethethe
8thethethethethe
9thethethethethethethe
10thethethethe


[root@1centos zhengzebiaodashi]# cat fg.txt 
2thethethe

将文件 gh.txt 的内容添加到包含 1 的行后

创建文件 gh.txt

[root@1centos zhengzebiaodashi]# vim ceshi.txt
zheliceshi
zheli1boke
~          


[root@1centos zhengzebiaodashi]# sed '/1/r ceshi.txt' the.txt 
1thethethe
zheliceshi
zheli1boke
2thethethe
3thethethethethe
4thethethethethethethe
5thethethethe
6hethethe
7thethethe
8thethethethethe
9thethethethethethethe
10thethethethe
zheliceshi
zheli1boke

在第 5 行下面插入内容 YOUR

[root@1centos zhengzebiaodashi]# sed '5aYOUR' the.txt 
1thethethe
2thethethe
3thethethethethe
4thethethethethethethe
5thethethethe
YOUR
6hethethe
7thethethe
8thethethethethe
9thethethethethethethe
10thethethethe

在包含 7 的行下插入内容 DATA

[root@1centos zhengzebiaodashi]# sed '/7/aDATA' the.txt 
1thethethe
2thethethe
3thethethethethe
4thethethethethethethe
5thethethethe
6hethethe
7thethethe
DATA
8thethethethethe
9thethethethethethethe
10thethethethe

在第 3 行下面插入多行内容

\n 换行

[root@1centos zhengzebiaodashi]# sed '3aDIYIHANG\nDIERHANG' the.txt 
1thethethe
2thethethe
3thethethethethe
DIYIHANG
DIERHANG
4thethethethethethethe
5thethethethe
6hethethe
7thethethe
8thethethethethe
9thethethethethethethe
10thethethethe

调用脚本

以上 输出 删除 替换 和 迁移 都可调用脚本
创建一个 脚本

[root@1centos zhengzebiaodashi]# vim sed.sh 
#!/bin/bash
2s/the/QWER/3          #将第二行第三个  the  替换成 THE 
3d                                 #删除第三行
4s/the/THE/g              #将第四行全部的 the 替换成 THE
2{H;d};9G                    #将第二行 迁移到第九行下面

调用

[root@1centos zhengzebiaodashi]# sed -f sed.sh the.txt 
1thethethe
4THETHETHETHETHETHETHE
5thethethethe
6hethethe
7thethethe
8thethethethethe
9thethethethethethethe

2thetheQWER
10thethethethe

直接操作文件

将 http 王爷服务的域名改成 www.ora.com
找到这行,记住
在这里插入图片描述

[root@1centos zhengzebiaodashi]# sed -i '/^#ServerName www/s/example/ora/' httpp.conf 
[root@1centos zhengzebiaodashi]# sed -i '/^#ServerName www/s/#//' httpp.conf 

更改完成
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值