一、sed讲解
sed是一个流编辑器, 非交互式的编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space)接着用 sed 命令处理缓冲区中的内容,处理完成后, 把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。
文件内容并没有改变,除非你 使用重定向存储输出。Sed 要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。
二、sed参数
sed - n-p-----------(把sed处理过的内容,打印)
-r -----------------(扩展正则的时候用)
-i---------------(确认修改内容)(也可以备份文件)
-e----------------(允许多次执行命令)
-s------------------(替换)
g-----------------(全局)
sed命令行参数
a 在当前行后添加一行或多行
c 在当前行进行替换修改
d 在当前行进行删除操作
i 在当前行之前插入文本
p 打印匹配的行或指定行
n 读入下一输入行,从下一条命令进行处理
! 对所选行以外的所有行应用命令
h 把模式空间里的内容重定向到暂存缓冲区
H 把模式空间里的内容追加到暂存缓冲区
g 取出暂存缓冲区的内容,将其复制到模式空间,覆盖该处原有内容
G 取出暂存缓冲区的内容,将其复制到模式空间,追加在原有内容后面
三、sed实战
1、删除1-9行
sed '1,9d' test.txt
2、将nginx替换成www
sed 's#nginx#www#g' test.txt
3、删除1-9行并且将nginx替换成www
sed '1,9d' test.txt|sed 's#nginx#www#g'
sed -e '1,9d' -e 's#nginx#www#g' test.txt
4、打印匹配nginx的行
sed -n '/nginx/p' test.txt
5、打印第二行的内容
sed -n '2p' test.txt
6、打印最后一行
sed -n '$p' test.txt
7、给第30行追加listen 80;
sed -i '30a listen 80 ;' /etc/nginx/nginx.conf
8、对第7行进行替换
sed -i '7c SELINUX=Disabled' /etc/selinux/config
9、非交互式修改指定的 配置文件
[root@Shell ~]# sed -ri '/UseDNS/cUseDNS no' /etc/ssh/sshd_config
[root@Shell ~]# sed -ri '/GSSAPIAuthentication/c#GSSAPIAuthentication no' /etc/ssh/sshd_config
[root@Shell ~]# sed -ri '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config
10、删除3行到最后一行
sed '3,$d' test.txt
11、删除最后一行
sed '$d' test.txt
12、删除所有行
sed '1,$d' test.txt
13、删除匹配到的行
sed /nginx/d test.txt
14、在30行上面插一行内容
sed -i '30i listen 80;' /etc/nginx/nginx.conf
15、将匹配到的行写入到test.conf中
sed -n '/nginx/w test.conf' /etc/nginx/nginx.conf
16、将passwd中的第二行写入到test.txt文件中
sed -n '2w test.txt' passwd
17、匹配root行,删除root行的下一行
sed '/root/ {n;d}' passwd
18、将root行下边的bin字符串替换成xxx
sed '/root/ {n; s/bin/xxx/}' test.txt
19、将第一行的内容写入到暂存区,并替换最后一行的内容
sed '1h;$g' test.txt
20、将第一行的内容写入到暂存区,在最后一行的下面调用暂存区的内容
[root@1 liangzeyu]# sed '1h;$G' test.txt
21、 将第一行的内容删除但保留至暂存区,在最后一行调用暂存区内容追加至尾部
sed -r '1{h;d};$G' /etc/hosts
22、将第一行的内容写入至暂存区,从第三行开始进行替换至尾部(从第三行的内容到尾部全部替换成第一行)
sed -r '1h;3,$g' test.txt
23、除了第三行,其他全部删除
sed -r '3!d' test.txt
24、sed匹配替换
s 替换命令标志
g 行内全局替换
i 忽略替换大小写
替换每一行出现的第一个root(#号后面不加参数默认只替换每行的第一个)
[root@1 liangzeyu]# sed 's#root#liangzeyu#' test.txt
liangzeyu root
binbash liangzeyu
26、 替换以root开头的行,进行替换第一个root
[root@1 liangzeyu]# sed 's#^root#liangzeyu#' test.txt
liangzeyu root
binbash root
27、查找匹配到的行,在匹配到的行后面添加内容
[root@1 liangzeyu]# sed -r 's#root$#& is lnb#' test.txt
root test root is lnb
binbash root is lnb
28、匹配包含有root的行进行替换(全局替换)
[root@1 liangzeyu]# sed -r 's#root#test#g' test.txt
test test test
binbash test
29、匹配包含有root的行进行全局替换,忽略大小写
sed -r 's#root#test#gi' test.txt
30、 关于sed的后向引用
[root@1 liangzeyu]# sed -r 's#(root)#\1-liangzeyu#g' test.txt
root-liangzeyu test root-liangzeyu
binbash root-liangzeyu
31、已sed后向引用的方式取出ip
[root@1 liangzeyu]# ifconfig eth0|sed -n '2p'|sed -r 's#(^.*net)(.*)(net.*$)#\2#g'
172.17.0.13
32、删除文件中的/etc/nginx/nginx.conf 的行
[root@1 liangzeyu]# sed -r '\/etc\/nginx\/nginx.conf/d' test.txt
root test root
binbash root
如果碰到/符号,建议用#符替换
sed -r 's#/etc/nginx/nginx.conf#/dev/null#' test.txt
sed -r 's#/etc/nginx/nginx.conf##g' test.txt
33、删除配置文件已#注释的行
sed '/^#/d' /etc/nginx/nginx.conf
34、删除配置文件中#开头的行,#号前面有空格或者tab(6个空格)
[root@1 liangzeyu]# sed -r '/^[ \t]*#/d' test.txt
root test root
/etc/nginx/nginx.conf
35、删除空行
sed -r '/^[ \t]*$/' /etc/nginx/nginx.conf
36、删除注释行和空行
[root@1 liangzeyu]# sed -r '/^[ \t]*#/d; /^[ \t]*$/d' test.txt
root test root
/etc/nginx/nginx.conf[root@1 liangzeyu]# sed -r '/^[ \t]*#|^[ \t]*$/d' test.txt
root test root
/etc/nginx/nginx.conf
[root@1 liangzeyu]# sed -r '/^[ \t]*(#|$)/d' test.txt
root test root
/etc/nginx/nginx.conf
37、 对文件的2-6行添加注释信息
[root@1 liangzeyu]# sed '2,6s/^/#/g' test.txt
root test root
##binbash root
#/etc/nginx/nginx.conf
#
38、对文件所有行添加注释信息
[root@1 liangzeyu]# sed 's/^/#/g' test.txt
#root test root
##binbash root
#/etc/nginx/nginx.conf
#
# sed -r '30,50s/^[ \t]*#*/#/' /etc/nginx.conf
# sed -r '2,8s/^[ \t#]*/#/' /etc/nginx.conf
有问题请加博主微信进行沟通!
全部评论