电子说
一、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的行前插入you are 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的行后追加you are 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
审核编辑:刘清
全部0条评论
快来发表一下你的评论吧 !