sed命令
文章目录
- 一、sed工作流程
- 二、sed使用方法
- 三、sed的脚本使用
一、sed工作流程
1、首先sed把当前正在处理的行保存在一个临时缓存区中(也称为模式空间),然后处理临时缓存区中的行,完成后把该行发送到屏幕上。
2、sed把每一行都存在临时缓存区中,对这个副本进行编辑,所以不会修改源文件。
3、sed主要用来自动编辑一个或多个文件,简化对文件的反复操作,编写转换程序等。
二、sed使用方法
- sed的常见语法格式有两种,一种叫命令行模式,另一种叫脚本模式。
1、命令行模式,格式:sed [option] ‘sed的命令|地址定位’ filename
option:1、-e 进行多项编辑,即对输入行应用多条sed命令时使用2、-n 取消默认的输出3、-f 指定sed脚本的文件名4、-r 使用扩展正则表达式5、-i inplace,原地编辑(修改源文件)常用命令和选项:1、p 打印行[root@localhost ~]# sed -n \'1p\' 2.txt #打印第一行root:x:0:0:root:/root:/bin/bash[root@localhost ~]# sed -n \'2p\' 2.txt #打印第二行bin:x:1:1:bin:/bin:/sbin/nologin[root@localhost ~]# sed -n \'1,3p\' 2.txt #打印第一到第三行root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin2、d 删除行[root@localhost ~]# sed \'1d\' 2.txt #删除第一行bin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin3、i 在当前行之前插入文本,多行时除最后一行外,每行末尾需用“\\”续行[root@localhost ~]# sed \"1ihello\" 2.txt #在第一行前插入hellohelloroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin[root@localhost ~]# sed \"1ihello\\nworld\" 2.txt #在第一行之前插入hello和world,通过\\n换行helloworldroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin4、a 在当前行后添加一行或多行,多行时除最后一行外,每行末尾需用“\\”续行[root@localhost ~]# sed \"3ahello\" 2.txt #在第3行之后插入helloroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinhelloadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin[root@localhost ~]# sed \'$ahello\' 2.txt #在最后一行插入helloroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinhello5、c 用此符号后的新文本替换当前行中的文本,多行时除最后一行外,每行末尾需用“\\”需行,整行替换[root@localhost ~]# sed \'2chello world\' 2.txt #将第2行整行替换为hello worldroot:x:0:0:root:/root:/bin/bashhello worlddaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin[root@localhost ~]# sed \'/root/chello world\' 2.txt #匹配root行,替换为hello worldhello worldbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin6、r 从文件中读取输入行[root@localhost ~]# sed \'3r /etc/hosts\' 2.txt #将/etc/hosts输入到第3行之后root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6adm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin7、w 将所选的行写入文件[root@localhost ~]# sed \'1,3w a.txt\' 2.txt #将2.txt文件中1到3行的内容输出到a.txt8、! 对所选行以外的所有行应用命令,放到行数之后[root@localhost ~]# sed -n \'4!p\' 2.txt #打印除第4行之外的所有行root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin9、s 用一个字符串替换另一个[root@localhost ~]# sed -n \'s/root/ROOT/p\' 2.txt #将root替换为大写的ROOT,但是只是替换首个匹配到的rootROOT:x:0:0:root:/root:/bin/bash[root@localhost ~]# sed -n \'1,2s/^/#/p\' 2.txt #将1和2行的行首加上#号,可以用作批量注释#root:x:0:0:root:/root:/bin/bash#bin:x:1:1:bin:/bin:/sbin/nologin10、g 在行内进行全局替换[root@localhost ~]# sed -n \'s/root/ROOT/gp\' 2.txt #加上g表示进行全局替换ROOT:x:0:0:ROOT:/ROOT:/bin/bash11、& 代表前面匹配到的整个内容,允许你在替换时引用原匹配文本[root@localhost ~]# echo \"192.168.1.1\" | sed \'s/[0-9.]*/&_backup/\'# 输出:192.168.1.1_backup12、= 打印行号
三、sed的脚本使用
1、两种执行方法[root@localhost ~]# sed -f scripts.sed file [root@localhost ~]# ./1.sed file #建议使用2、脚本写法注意事项:1)脚本文件是一个sed的命令行清单2)在每行的末尾不能有任何空格、制表符或其他文本3)如果在一行中有多个命令,应该用分号分隔4)不需要且不可用引号保护命令5)井号开头的行为注释[root@localhost ~]#cat 1.sed#!/bin/sed -f1,5ds/root/hello/g3i7775i888p[root@localhost ~]#sed -f 1.sed -i 2.txt #加上-i对源文件进行更改[root@localhost ~]# ./1.sed -i 2.txt