开发者社区> 技术小胖子> 正文

linux下sed的使用(中)

简介:
+关注继续查看

    linux下sed的使用(中)

本篇主要讲解:

---sed文本块的处理

一、sed文本块的处理

1.sed文本块处理的基本用法

 常用的处理选项有:

1
2
3
i 行前插入文本(insert)
a 行后插入文本(append)
c 替换当前行(change)

 需要插入多行文本内容时,一种方法是以“\n”表示换行,另一种是以“\”强制分隔。后面这种方法可能更符合阅读习惯。

 使用“&”可调用s替换操作中的整个查找串。

还是用测试文档:rclocal.txt

1
2
3
4
5
6
7
8
[root@svr5 ~]# cat rclocal.txt
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
6
touch /var/lock/subsys/local

1)行前插入文本 i

在第3行之前插入一行“Insert before”字符串:

1
2
3
4
5
6
7
8
9
[root@svr5 ~]# sed '3iInsert before' rclocal.txt
#!/bin/sh
#
Insert before
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
6
touch /var/lock/subsys/local

在最后一行之前插入一行“Insert before”字符串:

1
2
3
4
5
6
7
8
9
[root@svr5 ~]# sed '$iInsert before' rclocal.txt
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
6
Insert before
touch /var/lock/subsys/local

在包含“init”的每一行之前插入一行“xxxx”字符串:

1
2
3
4
5
6
7
8
9
10
11
[root@svr5 ~]# sed '/init/ixxxx' rclocal.txt
#!/bin/sh
#
.xxxx
# This script will be executed *after* all the other init scripts.
xxxx
# You can put your own initialization stuff in here if you don't
xxxx
# want to do the full Sys V style init stuff.
6
touch /var/lock/subsys/local

2)行后插入文本 a

在第3行之后插入一行“Insert after”字符串:

1
2
3
4
5
6
7
8
9
[root@svr5 ~]# sed '3aInsert after' rclocal.txt
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
Insert after
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
6
touch /var/lock/subsys/local

在最后一行之后追加一行“Insert after”字符串:

1
2
3
4
5
6
7
8
9
[root@svr5 ~]# sed '$aInsert after' rclocal.txt
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
6
touch /var/lock/subsys/local
Insert after

在包含“stuff”的每一行之后插入一行“xxxx”字符串:

1
2
3
4
5
6
7
8
9
10
[root@svr5 ~]# sed '/stuff/axxxx' rclocal.txt
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
xxxx
# want to do the full Sys V style init stuff.
xxxx
6
touch /var/lock/subsys/local

3)替换当前行 c

将第1行整行替换为“#!/bin/bash”:

1
2
3
4
5
6
7
8
[root@svr5 ~]# sed '1c#!/bin/bash' rclocal.txt
#!/bin/bash
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
6
touch /var/lock/subsys/local

将第1~4行整体替换为“#!/bin/bash”:

1
2
3
4
5
[root@svr5 ~]# sed '1,4c#!/bin/bash' rclocal.txt
#!/bin/bash
# want to do the full Sys V style init stuff.
6
touch /var/lock/subsys/local

将包含“/bin/sh”的每一行分别替换为“#!/bin/bash”:

1
2
3
4
5
6
7
8
[root@svr5 ~]# sed '/\/bin\/sh/c#!/bin/bash' rclocal.txt
#!/bin/bash
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
6
touch /var/lock/subsys/local

将包含单词“init”的每一行分别替换为“#!/bin/bash”:

1
2
3
4
5
6
7
8
[root@svr5 ~]# sed '/\<init\>/c#!/bin/bash' rclocal.txt
#!/bin/sh
#
#!/bin/bash
# You can put your own initialization stuff in here if you don't
#!/bin/bash
6
touch /var/lock/subsys/local

************注:\<表示已某单词开头、,  \>表示已某单词结尾 \<init\>就表示整个单词,像initital是不符合检索结果的*********************************

4)多行文本的处理

需要插入多行文本内容时,一种方法是以“\n”表示换行,另一种是以“\”强制分隔。后面这种方法可能更符合阅读习惯。以在第3行之前插入三行文本,内容依次为“xxxx”、“yyyy”、“zzzz”为例,下面的操作可以对比两种方法的效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@svr5 ~]# sed '3ixxxx\nyyyy\nzzzz' rclocal.txt         //方法1
#!/bin/sh
#
xxxx
yyyy
zzzz
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
6
touch /var/lock/subsys/local
[root@svr5 ~]# sed '3ixxxx\
> yyyy\
> zzzz' rclocal.txt                                         //方法2
#!/bin/sh
#
xxxx
yyyy
zzzz
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
6
touch /var/lock/subsys/local

2.利用sed文本块处理调整系统配置

1)修改主机名

主机名的配置文件位于/etc/sysconfig/network,主机名设置以“HOSTNAME”打头。

修改前:

1
2
3
4
[root@svr5 ~]# cat /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=svr5.tarena.com

将以“HOSTNAME”开头的行整行替换,设为“HOSTNAME=mysvr.example.org”:

1
[root@svr5 ~]# sed -i '/^HOSTNAME/cHOSTNAME=mysvr.example.org' /etc/sysconfig/network

确认替换结果:

1
2
3
[root@svr5 ~]# cat /etc/sysconfig/network NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=mysvr.example.org

2)添加hosts主机映射记录

在/etc/hosts文件最后一行后添加任务要求的2条映射记录:

1
2
[root@svr5 ~]# sed -i '$a192.168.4.5 svr5.example.com svr5\
> 119.75.217.56 www.baidu.com' /etc/hosts

验证添加效果:

1
2
3
[root@svr5 ~]# tail -2 /etc/hosts
192.168.4.5 svr5.example.com svr5
119.75.217.56 www.baidu.com

2.sed文本处理练习

先建立一个包含英文段落的测试文件,比如可使用/etc/nsswitch.conf文件。为了方便查看效果,我们将从这个文件中取第4~10行,并去掉开头的“# ”。开头的10行内容如下所示:

1
2
3
4
5
6
7
8
9
10
11
[root@svr5 ~]# head -10 /etc/nsswitch.conf
#
# /etc/nsswitch.conf
#
# An example Name Service Switch config file. This file should be
# sorted with the most-used services at the beginning.
#
# The entry '[NOTFOUND=return]' means that the search for an
# entry should stop if the search in the previous entry turned
# up nothing. Note that if the search failed due to some other reason
# (like no NIS server responding) then the search continues with the

截取操作及结果如下所示:

1
2
3
4
5
6
7
8
9
[root@svr5 ~]# sed -n '4,10p' /etc/nsswitch.conf | sed 's/# //' > nssw.txt
[root@svr5 ~]# cat nssw.txt
An example Name Service Switch config file. This file should be
sorted with the most-used services at the beginning.
#
The entry '[NOTFOUND=return]' means that the search for an
entry should stop if the search in the previous entry turned
up nothing. Note that if the search failed due to some other reason
(like no NIS server responding) then the search continues with the

本小节的操作即使用nssw.txt作为测试文件。

1)删除文件中每行的第二个、最后一个字符。

分两次替换操作,第一次替换掉第2个字符,第二次替换掉最后一个字符:

1
2
3
4
5
6
7
8
[root@svr5 ~]# sed 's/.//2;s/.$//' nssw.txt
A example Name Service Switch config file. This file should b
srted with the most-used services at the beginning
#
Te entry '[NOTFOUND=return]' means that the search for a
etry should stop if the search in the previous entry turne
u nothing. Note that if the search failed due to some other reaso
(ike no NIS server responding) then the search continues with th

2)删除文件中每行的第二个、最后一个单词。

分两次替换操作,第一次替换掉第2个单词,第二次替换掉最后一个单词:

1
2
3
4
5
6
7
8
[root@svr5 ~]# sed -r 's/[a-Z]+//2;s/[a-Z]+([^a-Z]*)$/\1/' nssw.txt
An Name Service Switch config file. This file should
sorted the most-used services at the .
#
The '[NOTFOUND=return]' means that the search for
entry stop if the search in the previous entry
up . Note that if the search failed due to some other
(like NIS server responding) then the search continues with

3)将文件中每行的第一个、第二个字符互换。

每行文本拆分为“第1个字符”、“第2个字符”、“剩下的所有字符”三个部分,然后通过替换操作重排顺序为“2-1-3”:

1
2
3
4
5
6
7
8
9
[root@svr5 ~]# sed -r 's/^(.)(.)(.*)/\2\1\3/' nssw.txt
nA example Name Service Switch config file. This file should be
osrted with the most-used services at the beginning.
#
hTe entry '[NOTFOUND=return]' means that the search for an
netry should stop if the search in the previous entry turned
pu nothing. Note that if the search failed due to some other reason
l(ike n up . Note that if the search failed due to some other
(like NIS server responding) then the search continues with

4)将文件中每行的第一个、第二个单词互换。

每行文本拆分为“第1个单词”、“单词分隔”、“第2个单词”、“剩下的所有字符”四个部分,然后通过替换操作重排顺序为“3-2-1-4”:

1
2
3
4
5
6
7
[root@svr5 ~]# sed -r 's/([a-Z]+)([^a-Z]*)([a-z]+)(.*)/\3\2\1\4/' nssw.txt example An Name Service Switch config file. This file should be
with sorted the most-used services at the beginning.
#
entry The '[NOTFOUND=return]' means that the search for an
should entry stop if the search in the previous entry turned
nothing up. Note that if the search failed due to some other reason
(no like NIS server responding) then the search continues with the

5)删除文件中所有的数字、行首的空格。

因原文件内没有数字,行首也没有空格,这里稍作一点处理,生成一个新测试文件:

1
2
3
4
5
6
7
8
9
[root@svr5 ~]# sed 's/o/o7/;s/l/l4/;3,5s/^/ /' nssw.txt > nssw2.txt
[root@svr5 ~]# cat nssw2.txt
An exampl4e Name Service Switch co7nfig file. This file should be
so7rted with the most-used services at the beginning.
#
.The entry '[NOTFOUND=return]' means that the search fo7r an
entry sho7ul4d stop if the search in the previous entry turned
up no7thing. Note that if the search fail4ed due to some other reason
(l4ike no7 NIS server responding) then the search continues with the

以nssw2.txt文件为例,删除所有数字、行首空格的操作如下:


1
2
3
4
5
6
7
8
1.[root@svr5 ~]# sed -r 's/[0-9]//g;s/^( )+//' nssw2.txt
2.An example Name Service Switch config file. This file should be
3.sorted with the most-used services at the beginning.
4.#
5.The entry '[NOTFOUND=return]' means that the search for an
6.entry should stop if the search in the previous entry turned
7.up nothing. Note that if the search failed due to some other reason
8.(like no NIS server responding) then the search continues with the

6)为文件中每个大写字母添加括号。

使用“&”可调用s替换操作中的整个查找串,所以可参考下列操作解决:

1
2
3
4
5
6
7
8
[root@svr5 ~]# sed 's/[A-Z]/(&)/g' nssw.txt
(A)n example (N)ame (S)ervice (S)witch config file. (T)his file should be
sorted with the most-used services at the beginning.
#
(T)he entry '[(N)(O)(T)(F)(O)(U)(N)(D)=return]' means that the search for an
entry should stop if the search in the previous entry turned
up nothing. (N)ote that if the search failed due to some other reason
(like no (N)(I)(S) server responding) then the search continues with the

或者:

1
2
3
4
5
6
7
8
[root@svr5 ~]# sed -r 's/([A-Z])/(\1)/g' nssw.txt
(A)n example (N)ame (S)ervice (S)witch config file. (T)his file should be
sorted with the most-used services at the beginning.
#
(T)he entry '[(N)(O)(T)(F)(O)(U)(N)(D)=return]' means that the search for an
entry should stop if the search in the previous entry turned
up nothing. (N)ote that if the search failed due to some other reason
(like no (N)(I)(S) server responding) then the search continues with the






      本文转自Jx战壕  51CTO博客,原文链接:http://blog.51cto.com/xujpxm/1391848,如需转载请自行联系原作者



版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
LINUX使用sed修改文件,如果包含变量,需要使用双引号
LINUX使用sed修改文件,如果包含变量,需要使用双引号
83 0
LINUX使用sed删除匹配行
LINUX使用sed删除匹配行
179 0
LINUX SHELL中使用sed匹配某一行并替换这一行的内容
LINUX SHELL中使用sed匹配某一行并替换这一行的内容
312 0
LINUX使用sed,字串中包含特殊字符怎么办?
LINUX使用sed,字串中包含特殊字符怎么办?
47 0
LINUX使用sed完成文本文件的修改
LINUX使用sed完成文本文件的修改
32 0
Linux Command sed 文本处理
Linux Command sed 文本处理
47 0
Linux sed命令增删改查 附代码
对应的命令进行查漏补缺以及更新知识点在起对应的进程时,每个进程都修改对应的sid(通过vim,交互式编辑),过于麻烦,有没有批量修改的语法或者语句(除了自已写一个脚本同步),答案是有的,那就是sed命令sed命令:流编辑模式,通过规则过滤来编辑数据本身该命令执行之后,会将其缓存区中的内容显示在屏幕中(实际内容其实没有改变,除非加入-i参数)group=99sid=123sid=123。......
46 0
Linux文本三剑客之sed编辑器(永远温柔永远清醒)(二)
Linux文本三剑客之sed编辑器(永远温柔永远清醒)(二)
59 0
Linux文本三剑客之sed编辑器(永远温柔永远清醒)(一)
Linux文本三剑客之sed编辑器(永远温柔永远清醒)(一)
44 0
+关注
技术小胖子
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
ECS运维指南 之 Linux系统诊断
立即下载
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
相关实验场景
更多
相关镜像