0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

sed用得少?sed常用语法简介

jf_TEuU2tls 来源:浩道Linux 2023-12-18 09:14 次阅读

一、sed简介

sed简称流编辑器,即stream editor的缩写。sed是一个操作、过滤和转换文本内容的强大工具。常用的功能有通过结合正则表达式对文件实现快速的增删改查。最常用的查询功能是过滤(过滤指定字符串)、取行(取出指定行)。

二、sed语法

(一)sed常用语法如下:

sed[选项参数]'[定位符]sed内置命令字符'文件

sed[选项参数]"[定位符]sed内置命令字符"文件

或通过执行命令以管道符输送给sed命令处理

命令 | sed [选项参数] '[定位符]sed内置命令字符' 文件
1、sed常用选项参数有:
-n:表示取消默认的sed输出,通常与sed内置命令p一起使用;
-i:表示直接将修改结果写入文件,如果不加-i,sed修改的是内存数据;
-e:表示多次编辑,不需要管道符号;
-r:表示支持正则表达式;
2、sed内置的命令字符,主要是用于对文件进行增删改查等操作,常见内置命令字符有:
a:表示对文本进行追加操作,在指定行后面添加一行或多行文本;
d:表示删除匹配行;
c:替换行;
i:表示插入文本,在指定行前添加一行或多行文本;
p:表示打印匹配行内容,通常与-n一同使用;
s/正则/替换内容/g:表示匹配正则内容,然后替换内容(支持正则表达式),结尾g表示全局匹配;
=:打印行号;
r:表示读取文件或者导入文件内容;
w:表示文件另存为或者导出文件内容;
3、sed匹配范围主要有:
空地址:表示全文处理;
单地址:表示指定文件某一行;
/pattern/:表示被模式匹配到的每一行;
范围区间:如12,22表示十二行到二十二行,如12,+5表示第12行向下5行;
步长:如1~2,表示1,3,5,7,9行;2~2,表示2,4,6,8,10行;

三、sed实例合集

以下例子针对sed语法进行举例,并且都是实际工作中常用到的剑客招式。

(一)通过定位符打印文件中指定的行:

首先准备一份文件sed_test.txt进行例子演示,该文件内容如下所示:

we are from haodaolinux
xiaoming is a good boy !
xiaohong is a girl .
we all like study .
I like linux
we stuty python
you study network
they study linux
liming like java
jam like mysql
tom like redis
例子1、通过sed命令打印sed_test.txt文件第4行的内容,命令执行如下:
[root@haodaolinux1 home]# sed -n '4p' sed_test.txt  
we all like study .

例子2、通过sed命令打印sed_test.txt文件第1行到第4行的内容,命令执行如下:

[root@haodaolinux1 home]# sed -n '1,4p' sed_test.txt 
we are from haodaolinux
xiaoming is a good boy !
xiaohong is a girl .
we all like study .
例子3、通过sed命令打印sed_test.txt文件第2行以及后面的4行的内容,命令执行如下:
[root@haodaolinux1 home]# sed -n '2,+4p' sed_test.txt  
xiaoming is a good boy !
xiaohong is a girl .
we all like study .
I like linux
we stuty python
例子4、通过sed命令打印sed_test.txt文件第1行开始,步长为2的所有内容,命令执行如下:
[root@haodaolinux1 home]# sed -n '1~2p' sed_test.txt          
we are from haodaolinux
xiaohong is a girl .
I like linux
you study network
liming like java
tom like redis
例子5、通过sed命令打印sed_test.txt文件以we开头的所有行,命令执行如下:
[root@haodaolinux1 home]# sed -rn '/^we/p' sed_test.txt 
we are from haodaolinux
we all like study .
we stuty python
例子6、通过sed命令打印sed_test.txt文件中有like的所有行,命令执行如下:
[root@haodaolinux1 home]# sed -rn '/like/p' sed_test.txt 
we all like study .
I like linux
liming like java
jam like mysql
tom like redis
例子7、通过sed命令打印sed_test.txt文件第1行,第3行,第5行的所有行,命令执行如下:
[root@haodaolinux1 home]# sed -n '1p;3p;5p' sed_test.txt 
we are from haodaolinux
xiaohong is a girl .
I like linux
例子8、通过sed命令打印sed_test.txt文件第3行以外的所有行,命令执行如下:
[root@haodaolinux1 home]# sed -n '3!p' sed_test.txt       
we are from haodaolinux
xiaoming is a good boy !
we all like study .
I like linux
we stuty python
you study network
they study linux
liming like java
jam like mysql
tom like redis

例子9、通过sed命令过滤内存信息,命令执行如下所示:

[root@haodaolinux1 home]# free -m | sed -n '/Mem/p'
Mem:            981         479         132           7         370         342
例子10、通过sed命令过滤磁盘根分区的信息,命令执行如下所示:
[root@haodaolinux1 home]# df -h | sed -n '//$/p'
/dev/sda2        38G   28G  9.9G  74% /

(二)删除指定文件中相关内容:

如果要对源文件进行删除操作,需要加上-i选项!!!

首先准备一份文件sed_test01.txt进行例子演示,该文件内容如下所示:

we are from haodaolinux and we are study linux .
#you do not like study ?
xiaoming is a good boy !
we all like study and we are the same and we are together !


I like linux
we stuty python , we like java , we like linux .
you study network
they study linux
liming like java
jam like mysql
tom like redis
#so we will get the python book.
例子1、通过sed命令删除sed_test01.txt文件第3行的内容,命令执行如下:
[root@haodaolinux1 home]# sed '3d' sed_test01.txt
we are from haodaolinux and we are study linux .
#you do not like study ?
we all like study and we are the same and we are together !


I like linux
we stuty python , we like java , we like linux .
you study network
they study linux
liming like java
jam like mysql
tom like redis
#so we will get the python book.
例子2、通过sed命令删除sed_test01.txt文件中以#开头的行,命令执行如下:
[root@haodaolinux1 home]# sed '/^#/d' sed_test01.txt
we are from haodaolinux and we are study linux .
xiaoming is a good boy !
we all like study and we are the same and we are together !


I like linux
we stuty python , we like java , we like linux .
you study network
they study linux
liming like java
jam like mysql
tom like redis
例子3、通过sed命令删除sed_test01.txt文件中不包含like的行,命令执行如下:
[root@haodaolinux1 home]# sed '/like/!d' sed_test01.txt 
#you do not like study ?
we all like study and we are the same and we are together !
I like linux
we stuty python , we like java , we like linux .
liming like java
jam like mysql
tom like redis
例子4、通过sed命令删除sed_test01.txt文件中空白的行,命令执行如下:
[root@haodaolinux1 home]# sed '/^$/d' sed_test01.txt
we are from haodaolinux and we are study linux .
#you do not like study ?
xiaoming is a good boy !
we all like study and we are the same and we are together !
I like linux
we stuty python , we like java , we like linux .
you study network
they study linux
liming like java
jam like mysql
tom like redis
#so we will get the python book.
例子5、通过sed命令将sed_test01.txt文件中所有行替换为haodaolinux,命令执行如下:
[root@haodaolinux1 home]# sed 'c haodaolinux' sed_test01.txt
haodaolinux
haodaolinux
haodaolinux
haodaolinux
haodaolinux
haodaolinux
haodaolinux
haodaolinux
haodaolinux
haodaolinux
haodaolinux
haodaolinux
haodaolinux
例子6、通过sed命令将sed_test01.txt文件中第3行替换为haodaolinux,命令执行如下:
[root@haodaolinux1 home]# sed '3c haodaolinux' sed_test01.txt
we are from haodaolinux and we are study linux .
#you do not like study ?
haodaolinux
we all like study and we are the same and we are together !


I like linux
we stuty python , we like java , we like linux .
you study network
they study linux
liming like java
jam like mysql
tom like redis
#so we will get the python book.
例子7、通过sed命令将sed_test01.txt文件中boy所在的行替换为boy=student,命令执行如下:
[root@haodaolinux1 home]# sed '/boy/c boy=student' sed_test01.txt
we are from haodaolinux and we are study linux .
#you do not like study ?
boy=student
we all like study and we are the same and we are together !


I like linux
we stuty python , we like java , we like linux .
you study network
they study linux
liming like java
jam like mysql
tom like redis
#so we will get the python book.
例子8、通过sed命令将sed_test01.txt文件中每一行的第1个we替换为they,命令执行如下:
[root@haodaolinux1 home]# sed 's/we/they/' sed_test01.txt 
they are from haodaolinux and we are study linux .
#you do not like study ?
xiaoming is a good boy !
they all like study and we are the same and we are together !


I like linux
they stuty python , we like java , we like linux .
you study network
they study linux
liming like java
jam like mysql
tom like redis
#so they will get the python book.
例子9、通过sed命令将sed_test01.txt文件中每一行的所有we替换为they,命令执行如下:
[root@haodaolinux1 home]# sed 's/we/they/g' sed_test01.txt
they are from haodaolinux and they are study linux .
#you do not like study ?
xiaoming is a good boy !
they all like study and they are the same and they are together !


I like linux
they stuty python , they like java , they like linux .
you study network
they study linux
liming like java
jam like mysql
tom like redis
#so they will get the python book.
例子10、通过sed命令将sed_test01.txt文件中每一行的第3个we替换为they,命令执行如下:
[root@haodaolinux1 home]# sed 's/we/they/3' sed_test01.txt
we are from haodaolinux and we are study linux .
#you do not like study ?
xiaoming is a good boy !
we all like study and we are the same and they are together !


I like linux
we stuty python , we like java , they like linux .
you study network
they study linux
liming like java
jam like mysql
tom like redis
#so we will get the python book.
例子11、通过sed命令将sed_test01.txt文件中每一行的所有we替换为(we),命令执行如下:
[root@haodaolinux1 home]# sed 's/we/(&)/g' sed_test01.txt 
(we) are from haodaolinux and (we) are study linux .
#you do not like study ?
xiaoming is a good boy !
(we) all like study and (we) are the same and (we) are together !


I like linux
(we) stuty python , (we) like java , (we) like linux .
you study network
they study linux
liming like java
jam like mysql
tom like redis
#so (we) will get the python book.
例子12、通过sed命令将sed_test01.txt文件中第4行的所有we替换为they,命令执行如下:
[root@haodaolinux1 home]# sed '4s/we/they/g' sed_test01.txt 
we are from haodaolinux and we are study linux .
#you do not like study ?
xiaoming is a good boy !
they all like study and they are the same and they are together !


I like linux
we stuty python , we like java , we like linux .
you study network
they study linux
liming like java
jam like mysql
tom like redis
#so we will get the python book.
例子13、通过sed命令将sed_test01.txt文件中第4行的所有we进行删除,命令执行如下:
[root@haodaolinux1 home]# sed '4s/we//g' sed_test01.txt 
we are from haodaolinux and we are study linux .
#you do not like study ?
xiaoming is a good boy !
 all like study and  are the same and  are together !


I like linux
we stuty python , we like java , we like linux .
you study network
they study linux
liming like java
jam like mysql
tom like redis
#so we will get the python book.
例子14、通过sed命令将sed_test01.txt文件中第4行的第1个we替换为they,并且只将该行打印输出,命令执行如下:
[root@haodaolinux1 home]# sed -n '4s/we/they/p' sed_test01.txt 
they all like study and we are the same and we are together !
例子15、通过sed命令将sed_test01.txt文件中第4行的行号打印输出,命令执行如下:
[root@haodaolinux1 home]# sed -n '4=' sed_test01.txt  
4
例子15、通过sed命令将sed_test01.txt文件中包含like的行的行号打印输出,命令执行如下:
[root@haodaolinux1 home]# sed -n '/like/=' sed_test01.txt     
2
4
6
7
10
11
12
例子16、通过sed命令将sed_test01.txt文件中将以java结尾的行的行号打印输出,命令执行如下:
[root@haodaolinux1 home]# sed -n '/java$/=' sed_test01.txt     
10
例子17、通过sed命令将sed_test01.txt文件中总的行号打印输出,命令执行如下:
[root@haodaolinux1 home]# sed -n '$=' sed_test01.txt        
13

(三)通过sed对文件中的多行文本进行处理:

如果要对源文件进行编辑操作,需要加上-i选项!!

首先准备一份文件sed_test02.txt进行例子演示,该文件内容如下所示:

linux   python  network linux
python  linux   c       mysql
java    html    php     go
go      mysql   python  python
php     c++     python  php
例子1、通过sed命令在sed_test02.txt文件中的第2行前插入we are study,命令执行如下:
[root@haodaolinux1 home]# sed '2i we are study' sed_test02.txt 
linux   python  network linux
we are study
python  linux   c       mysql
java    html    php     go
go      mysql   python  python
php     c++     python  php
例子2、通过sed命令在sed_test02.txt文件中有mysql的行前插入youare study,命令执行如下:
[root@haodaolinux1 home]# sed '/mysql/i you are study' sed_test02.txt       
linux   python  network linux
you are study
python  linux   c       mysql
java    html    php     go
you are study
go      mysql   python  python
php     c++     python  php
例子3、通过sed命令在sed_test02.txt文件中的第2行h后追加we are study too,命令执行如下:
[root@haodaolinux1 home]# sed '2a we are study too' sed_test02.txt         
linux   python  network linux
python  linux   c       mysql
we are study too
java    html    php     go
go      mysql   python  python
php     c++     python  php
例子4、通过sed命令在sed_test02.txt文件中有mysql的行后追加youare study too,命令执行如下:
[root@haodaolinux1 home]# sed '/mysql/a you are study too' sed_test02.txt  
linux   python  network linux
python  linux   c       mysql
you are study too
java    html    php     go
go      mysql   python  python
you are study too
php     c++     python  php
例子5、两个文件内容合并实例: 首先创建read.txt文件内容如下所示:
we are study
you are study
they are study
(1)通过sed命令读取read.txt文件的内容,将读取内容追加到sed_test02.txt文件中每一行的后面,命令执行如下:
[root@haodaolinux1 home]# sed 'r read.txt' sed_test02.txt  
linux   python  network linux
we are study
you are study
they are study
python  linux   c       mysql
we are study
you are study
they are study
java    html    php     go
we are study
you are study
they are study
go      mysql   python  python
we are study
you are study
they are study
php     c++     python  php
we are study
you are study
they are study
(2)通过sed命令读取read.txt文件的内容,将读取内容追加到sed_test02.txt文件中的第3行的后面,命令执行如下:
[root@haodaolinux1 home]# sed '3r read.txt' sed_test02.txt 
linux   python  network linux
python  linux   c       mysql
java    html    php     go
we are study
you are study
they are study
go      mysql   python  python
php     c++     python  php
(3)通过sed命令读取read.txt文件的内容,将读取内容追加到sed_test02.txt文件中有mysql行的后面,命令执行如下:
[root@haodaolinux1 home]# sed '/mysql/r read.txt' sed_test02.txt 
linux   python  network linux
python  linux   c       mysql
we are study
you are study
they are study
java    html    php     go
go      mysql   python  python
we are study
you are study
they are study
php     c++     python  php
例子6:文件内容另存为其它文件实例:

(1)通过sed命令读取sed_test02.txt文件中所有内容,另存为write.txt文件,命令执行如下:

[root@haodaolinux1 home]# sed 'w write.txt' sed_test02.txt 
linux   python  network linux
python  linux   c       mysql
java    html    php     go
go      mysql   python  python
php     c++     python  php
(2)通过sed命令将sed_test02.txt文件中包含mysql的行,另存为write_mysql.txt文件,命令执行如下:
[root@haodaolinux1 home]# sed -i '/mysql/w write_mysql.txt' sed_test02.txt
(3)通过sed命令将sed_test02.txt文件中1行到3行的内容,另存为write_13.txt文件,命令执行如下:
[root@haodaolinux1 home]# sed -i '2,3w write_13.txt' sed_test02.txt







审核编辑:刘清

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • Linux
    +关注

    关注

    87

    文章

    10990

    浏览量

    206735
  • 磁盘
    +关注

    关注

    1

    文章

    338

    浏览量

    24882
  • SED
    SED
    +关注

    关注

    0

    文章

    23

    浏览量

    26995

原文标题:sed用得少?那是你没发现它这些实用技巧~

文章出处:【微信号:浩道linux,微信公众号:浩道linux】欢迎添加关注!文章转载请注明出处。

收藏 人收藏

    评论

    相关推荐

    Linux中grep、sed和awk命令详解

    今天给大家聊一聊Linux中文本操作的`三剑客:awk、grep、sed`,因其功能强大、使用频繁,且是Linux下文本处理的得力利器,常被称之为`文本三剑客`。`grep`常用于查找,`sed`
    发表于 04-26 17:20 1934次阅读
    Linux中grep、<b class='flag-5'>sed</b>和awk命令详解

    Linux中sed命令用法

    这篇文章为初学者提供了关于 Linux 中 sed 命令的全面指南,涵盖了其历史、用途以及一些实用的技巧和窍门。通过掌握 sed,您可以高效处理文本处理任务,这对于任何使用 Linux 的人来说都是一项宝贵的技能。
    发表于 07-21 10:38 230次阅读
    Linux中<b class='flag-5'>sed</b>命令用法

    VHDL语言的常用语法

    VHDL语言的常用语法[学习要求] 掌握VHDL硬件描述语言的基本描述语句。并可以利用这些语句进行简单电路的设计。[重点与难点]重点:常用的并行语句与顺序语句的语法。难点:部件(Component
    发表于 03-19 16:45

    PCB常用语汇总

    PCB常用语汇总
    发表于 11-13 12:03

    快速理解linux流编辑器sed命令

    全文处理结束sed可做的编辑动作包括删除、查找替换、添加、插入、从其他文件中读入数据等常用场景(1)shell脚本中不便使用vi命令对文件进行编辑,sed命令则很方便(2)文件太大,用vi编辑器打开
    发表于 11-30 10:44

    linux学习大全之sed 命令详解

    定的script文件来处理输入的文本文件;-r∶sed 的动作支援的是延伸型正规表示法的语法;-i∶直接修改读取的档案内容,而不是由萤幕输出;-h或--help:显示帮助;-V或--version:显示版本
    发表于 01-12 15:20

    Linux的sed命令详解

    Linux命令大全 - sed命令
    发表于 03-13 16:25

    labsql ADO 常用语句命令

    labsqlADO 常用语句命令
    发表于 08-14 16:21

    SED的显示原理是什么?

    SED显示技术SED的基本显示原理同CRT相同,都是由电子撞击荧光材料而发光,但电子撞击的方式却不一样。
    发表于 09-27 09:01

    Linux中Sed常用操作有哪些

    Linux中Sed常用操作
    发表于 05-26 10:53

    单片机C语言编程常用语句有哪些?

    单片机C语言编程常用语句有哪些?
    发表于 10-21 08:45

    SED1520中文资料,SED1520中文数据手册

    内藏 SED1520 控制器,点阵图形液晶显示模块,使用手册 前 言-------------------------------------------------------2注意事项--------------------------------------------------------2第一章
    发表于 09-07 22:04 160次下载

    SED显示技术,SED显示技术原理是什么?

    SED显示技术,SED显示技术原理是什么?     谈到平板显示技术,多数人可能只知道液晶和等离子,有人可能还知道有机发光
    发表于 03-27 11:56 3946次阅读

    关于Linux中的sed简易介绍与工作原理

    熟悉 Linux 的同学一定知道大名鼎鼎的 Linux 三剑客,它们是 grep、awk、sed,我们今天要聊的主角就是 sed
    的头像 发表于 03-26 15:35 1995次阅读
    关于Linux中的<b class='flag-5'>sed</b>简易介绍与工作原理

    MySQL常用语

    MySQL是一个关系型数据库管理系统,广泛应用于Web应用程序的开发以及数据管理领域。在使用MySQL时,有一些常用的语句可以帮助我们进行数据的操作和管理。接下来,我将详细介绍MySQL的常用语
    的头像 发表于 11-21 11:11 271次阅读