一、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 haodaolinuxxiaoming is a good boy !xiaohong is a girl .we all like study .i like linuxwe stuty pythonyou study networkthey study linuxliming like javajam like mysqltom 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 haodaolinuxxiaoming 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 linuxwe stuty python例子4、通过sed命令打印sed_test.txt文件第1行开始,步长为2的所有内容,命令执行如下:[root@haodaolinux1 home]# sed -n '1~2p' sed_test.txt we are from haodaolinuxxiaohong is a girl .i like linuxyou study networkliming like javatom like redis例子5、通过sed命令打印sed_test.txt文件以we开头的所有行,命令执行如下:[root@haodaolinux1 home]# sed -rn '/^we/p' sed_test.txt we are from haodaolinuxwe 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 linuxliming like javajam like mysqltom like redis例子7、通过sed命令打印sed_test.txt文件第1行,第3行,第5行的所有行,命令执行如下:[root@haodaolinux1 home]# sed -n '1p;3p;5p' sed_test.txt we are from haodaolinuxxiaohong 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 haodaolinuxxiaoming is a good boy !we all like study .i like linuxwe stuty pythonyou study networkthey study linuxliming like javajam like mysqltom 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 linuxwe stuty python , we like java , we like linux .you study networkthey study linuxliming like javajam like mysqltom like redis#so we will get the python book.例子1、通过sed命令删除sed_test01.txt文件第3行的内容,命令执行如下:[root@haodaolinux1 home]# sed '3d' sed_test01.txtwe 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 linuxwe stuty python , we like java , we like linux .you study networkthey study linuxliming like javajam like mysqltom like redis#so we will get the python book.例子2、通过sed命令删除sed_test01.txt文件中以#开头的行,命令执行如下:[root@haodaolinux1 home]# sed '/^#/d' sed_test01.txtwe 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 linuxwe stuty python , we like java , we like linux .you study networkthey study linuxliming like javajam like mysqltom 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 linuxwe stuty python , we like java , we like linux .liming like javajam like mysqltom like redis例子4、通过sed命令删除sed_test01.txt文件中空白的行,命令执行如下:[root@haodaolinux1 home]# sed '/^$/d' sed_test01.txtwe 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 linuxwe stuty python , we like java , we like linux .you study networkthey study linuxliming like javajam like mysqltom like redis#so we will get the python book.例子5、通过sed命令将sed_test01.txt文件中所有行替换为haodaolinux,命令执行如下:[root@haodaolinux1 home]# sed 'c haodaolinux' sed_test01.txthaodaolinuxhaodaolinuxhaodaolinuxhaodaolinuxhaodaolinuxhaodaolinuxhaodaolinuxhaodaolinuxhaodaolinuxhaodaolinuxhaodaolinuxhaodaolinuxhaodaolinux例子6、通过sed命令将sed_test01.txt文件中第3行替换为haodaolinux,命令执行如下:[root@haodaolinux1 home]# sed '3c haodaolinux' sed_test01.txtwe are from haodaolinux and we are study linux .#you do not like study ?haodaolinuxwe all like study and we are the same and we are together !i like linuxwe stuty python , we like java , we like linux .you study networkthey study linuxliming like javajam like mysqltom 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.txtwe are from haodaolinux and we are study linux .#you do not like study ?boy=studentwe all like study and we are the same and we are together !i like linuxwe stuty python , we like java , we like linux .you study networkthey study linuxliming like javajam like mysqltom 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 linuxthey stuty python , we like java , we like linux .you study networkthey study linuxliming like javajam like mysqltom 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.txtthey 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 linuxthey stuty python , they like java , they like linux .you study networkthey study linuxliming like javajam like mysqltom 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.txtwe 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 linuxwe stuty python , we like java , they like linux .you study networkthey study linuxliming like javajam like mysqltom 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 networkthey study linuxliming like javajam like mysqltom 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 linuxwe stuty python , we like java , we like linux .you study networkthey study linuxliming like javajam like mysqltom 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 linuxwe stuty python , we like java , we like linux .you study networkthey study linuxliming like javajam like mysqltom 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 2467101112例子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 linuxpython linux c mysqljava html php gogo mysql python pythonphp 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 linuxwe are studypython linux c mysqljava html php gogo mysql python pythonphp c++ python php例子2、通过sed命令在sed_test02.txt文件中有mysql的行前插入you are study,命令执行如下:[root@haodaolinux1 home]# sed '/mysql/i you are study' sed_test02.txt linux python network linuxyou are studypython linux c mysqljava html php goyou are studygo mysql python pythonphp 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 linuxpython linux c mysqlwe are study toojava html php gogo mysql python pythonphp c++ python php例子4、通过sed命令在sed_test02.txt文件中有mysql的行后追加you are study too,命令执行如下:[root@haodaolinux1 home]# sed '/mysql/a you are study too' sed_test02.txt linux python network linuxpython linux c mysqlyou are study toojava html php gogo mysql python pythonyou are study toophp c++ python php例子5、两个文件内容合并实例: 首先创建read.txt文件内容如下所示:we are studyyou are studythey are study(1)通过sed命令读取read.txt文件的内容,将读取内容追加到sed_test02.txt文件中每一行的后面,命令执行如下:[root@haodaolinux1 home]# sed 'r read.txt' sed_test02.txt linux python network linuxwe are studyyou are studythey are studypython linux c mysqlwe are studyyou are studythey are studyjava html php gowe are studyyou are studythey are studygo mysql python pythonwe are studyyou are studythey are studyphp c++ python phpwe are studyyou are studythey are study(2)通过sed命令读取read.txt文件的内容,将读取内容追加到sed_test02.txt文件中的第3行的后面,命令执行如下:[root@haodaolinux1 home]# sed '3r read.txt' sed_test02.txt linux python network linuxpython linux c mysqljava html php gowe are studyyou are studythey are studygo mysql python pythonphp 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 linuxpython linux c mysqlwe are studyyou are studythey are studyjava html php gogo mysql python pythonwe are studyyou are studythey are studyphp c++ python php例子6:文件内容另存为其它文件实例:
(1)通过sed命令读取sed_test02.txt文件中所有内容,另存为write.txt文件,命令执行如下:
[root@haodaolinux1 home]# sed 'w write.txt' sed_test02.txt linux python network linuxpython linux c mysqljava html php gogo mysql python pythonphp 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
一种带辅助变压器的Flyback变换器ZVS软开关实现方案
【节能学院】安科瑞能耗监测系统在吴忠市回医药基地的研究与应用
电池切换电路制3V的系统-Battery-Switchove
努比亚Z17mini手机评测:双摄实力媲美旗舰
传统以太网是如何工作的呢?传统以太网和时间敏感网络TSN的区别
sed用得少?sed常用语法简介
新唐科技W584B080主板介绍
二极管电路分析方法讲解
Codasip宣布新人事任命 满足客户对可定制RISC-V处理器的强劲需求
币圈是如何借助区块链死灰复燃的
供应链管理系统的优点是怎样的
关于光伏系统损失的发电量的介绍和应用
瞄准3D芯片发展新趋势,Mentor戮力拓展EDA市场版图
智慧园区系统如何建设,规划方案又是怎样的
华大九天依托自身EDA软件平台资源,推出一站式晶圆制造工程服务
锂电池可靠性测试
中国首颗通信能力达10Gbps的民营5G低轨宽带卫星正式开通
给年轻电子工程师的十大忠告
PCB、SMT、PCBA三者的区别
y电容和瓷片电容的区别有哪些?