shell脚本之sed

目录

1、sed概述

(1)sed命令与正则表达式结合使用的示例

(2)删除符合条件的文本

(3)替换符合条件的文本

 (4)迁移符合条件的文本

 (5)使用脚本编辑文件


前言

在Linux/UNIX系统中包含很多种类的文本处理器或文本编辑器,其中包括我们之前学习过的VIM编辑器与grep等,而grep,sed,awk更是shell编程中经常用到的文本处理工具,被称之为shell编程三剑客

1、sed概述

文本处理工具,读取文本内容,根据指定的条件进行处理,如删除、替换、添加等

■可在无交互的情况下实现相当负载的文本处理操作

■被广泛应用于shell脚本,以完成自动化处理任务

■sed依赖于正则表达式

■工作原理 读取 --->> 执行 --->> 显示

注:默认情况下所有的sed命令都是在模式空间内执行的,因此输入的文件并不会发生任何变化,除非是用重定向存储输出。

1、sed命令常见用法

通常情况下调用sed命令有两种格式,如下所示。其中,“参数”是指操作的目标文件,当存在多个操作对象时用,文件之间用逗号","表示通过指定的脚本文件来处理输入的目标文件。

sed [选项]  '操作' 参数

sed [选项]  -f scriptfile 参数

-e(expression)表示用指定命令或者脚本来处理输入的文本文件
-f(file)表示用指定的脚本文件来处理输入的文本文件
-h(help)显示帮助

-n

表示仅显示处理后的结果
-i直接编辑文本文件
-r-E    使用扩展正则表达式
-s将多个文件是为独立文件,而不是单个连续的长文件流

“操作”用于制定对文件操作的动作行为,也就是sed的命令。通常情况下是采用的"[n1[,n2]]"操作参数的格式。n1、n2是可选的,代表选择进行操作的行数,如操作需要早5~20之间进行,则表示为"5,20"动作行为。常见的操作包括以下几种。

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

示例:

这时test.txt的内容

[root@localhost /]# sed -n '3p' test.txt   输出第三行
The home of Football on BBC Sport online.
[root@localhost /]# sed -n '3,5p' test.txt       打印三到五行
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.
[root@localhost /]# sed -n 'p;n' test.txt          打印奇数行 n表示读入下一行资料
he was short and fat.
The home of Football on BBC Sport online.
google is the best tools for search keyword.
PI=3.141592653589793238462643383249901429
Actions speak louder than words

#woooooood 
I bet this place is really spooky late at night!
[root@localhost /]# sed -n 'n;p' test.txt         打印偶数行   n表示读入下一行资料
He was wearing a blue polo shirt with black pants. 
the tongue is boneless but it breaks bones.12! 
The year ahead will test our political establishment to the limit.
a wood cross!

#woood #
# AxyzxyzxyzxyzC
Misfortunes never come alone/single.
[root@localhost /]# sed -n '1,5{p;n}'  test.txt   输出第一1~5行之间的奇数行(第1、3、5行)
he was short and fat.
The home of Football on BBC Sport online.
google is the best tools for search keyword.
[root@localhost /]# sed -n '10,${n;p}'  test.txt   输出第10行文件尾之间的偶数行

#woooooood 
I bet this place is really spooky late at night! 
[root@localhost /]# ifconfig ens33 | sed -n 3p        显示ifconfig第三行内容
        inet6 fe80::ec2c:2d13:8a08:5479  prefixlen 64  scopeid 0x20<link>

在执行"sed -n ‘10’,${n,p}'test.txt"命令时,读取的第一行是文件的第10行,读取的第2行是文件的第11行,以此类推,所以输出的偶数行是文件的第11行、13行直至文件结尾,其中包括行。

以上是sed命令的基本用法,sed命令结合正则表达式时,格式略有不同,正则表达式以"/"包围。

(1)sed命令与正则表达式结合使用的示例

[root@localhost /]# sed -n '/the/p' test.txt          输出包含the的行
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@localhost /]# sed -n '4,/the/p' test.txt      输出从第四行至第一个包含the的行
the tongue is boneless but it breaks bones.12! 
google is the best tools for search keyword.
[root@localhost /]# sed -n '/the/=' test.txt   输出包含the的行所在的行号,等号(=)用来输出行号
4
5
6
[root@localhost /]# sed -n '/^PI/p' test.txt  输出以PI开头的行 
PI=3.141592653589793238462643383249901429 
[root@localhost /]# sed -n '/[0-9]$/p' test.txt    输出以数字结尾的行
PI=3.141592653589793238462643383249901429
[root@localhost /]# sed -n '/\<wood\>/p' test.txt    输出包含单词wood的行,\<、\>代表单词边界
a wood cross!

(2)删除符合条件的文本

因为后面的示例还需要使用测试文件test.txt,所以在执行删除操作之前需要先将测试文件备份。一下实例分别演示了sed命令的几种常用删除用法。下面命令中n1命令用于计算文件的行数,结合该命令可以更加直观地查看到命令执行的结果。

[root@localhost /]# nl test.txt | sed '3d'      删除test.txt中的第三行
     1	he was short and fat.
     2	He was wearing a blue polo shirt with black pants. 
     4	the tongue is boneless but it breaks bones.12! 
     5	google is the best tools for search keyword.
     6	The year ahead will test our political establishment to the limit.
     7	PI=3.141592653589793238462643383249901429
     8	a wood cross!
     9	Actions speak louder than words
       
       
    10	#woood #
    11	#woooooood 
    12	# AxyzxyzxyzxyzC
    13	I bet this place is really spooky late at night! 
    14	Misfortunes never come alone/single.
[root@localhost /]# nl test.txt | sed '3,5d'       删除第三到五行
     1	he was short and fat.
     2	He was wearing a blue polo shirt with black pants. 
     6	The year ahead will test our political establishment to the limit.
     7	PI=3.141592653589793238462643383249901429
     8	a wood cross!
     9	Actions speak louder than words
       
       
    10	#woood #
    11	#woooooood 
    12	# AxyzxyzxyzxyzC
    13	I bet this place is really spooky late at night! 
    14	Misfortunes never come alone/single.
[root@localhost /]# nl test.txt | sed '/cross/d'      删除带有cross的行,可以用!进行取反
     1	he was short and fat.
     2	He was wearing a blue polo shirt with black pants. 
     3	The home of Football on BBC Sport online.
     4	the tongue is boneless but it breaks bones.12! 
     5	google is the best tools for search keyword.
     6	The year ahead will test our political establishment to the limit.
     7	PI=3.141592653589793238462643383249901429
     9	Actions speak louder than words
       
       
    10	#woood #
    11	#woooooood 
    12	# AxyzxyzxyzxyzC
    13	I bet this place is really spooky late at night! 
    14	Misfortunes never come alone/single.
[root@localhost /]# sed '/^[a-z]/d' test.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


#woood #
#woooooood 
# AxyzxyzxyzxyzC
I bet this place is really spooky late at night! 
Misfortunes never come alone/single.
[root@localhost /]# sed '/\.$/d' test.txt   删除以点为结尾的行  \是转义符号
He was wearing a blue polo shirt with black pants. 
the tongue is boneless but it breaks bones.12! 
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words


#woood #
#woooooood 
# AxyzxyzxyzxyzC
I bet this place is really spooky late at night! 

(3)替换符合条件的文本

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

示例:

1、 

 2、

3、

4、 

 5、

 6、

7、 

 8、

 9、

 (4)迁移符合条件的文本

在使用sed命令迁移符合条件的文本时,常用到以下参数。

g、G将剪切板中的数据覆盖
w保存为文件
r读取指定文件
a追加指定内容。
H复制到剪切板
I,i忽略大小写

例子1

 例2

 例子3

 例子4

 例子5

 例子6  在包含the的每一行后插入一个新行

 例子7

 (5)使用脚本编辑文件

使用sed脚本将多个编辑指令存放到文件中(每一条编辑指令),通过"-f"选项来调用。

例子1

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值