Linux shell之 sed的基本用法

问题

本案例要求熟悉sed命令的p、d、s等常见操作,并结合正则表达式,完成以下任务:
删除文件中每行的第二个、最后一个字符
删除文件中每行的第二个、最后一个单词
将文件中每行的第一个、第二个字符互换
将文件中每行的第一个、第二个单词互换
删除文件中所有的数字、行首的空格
为文件中每个大写字母添加括号
方案
sed文本处理工具的用法:
用法1:前置命令 | sed [选项] ‘编辑指令’
用法2:sed [选项] ‘编辑指令’ 文件… …
相关说明如下:
其中,“编辑指令”用来实现过滤操作,由“[定址符]处理动作”组成
而“定址符”用来定义需要操作的文本,由“[地址1 [,地址2]]组成,地址1为起始位置,地址2为结束位置
未指定“定址符”时,默认处理所有文本
只指定地址1时,只处理与地址1相匹配的文本
步骤一:认识sed工具的基本选项
1)sed命令的 -n 选项
执行p打印等过滤操作时,希望看到的是符合条件的文本。但不使用任何选项时,默认会将原始文本一并输出,从而干扰过滤效果。比如,尝试用sed输出/etc/rc.local的第1行:

[root@svr5 ~]# sed '1p' /etc/rc.local
#!/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.

touch /var/lock/subsys/local

可以发现所有的行都被显示出来了。—— 正确的用法应该添加 -n 选项,这样就可以只显示第1行了:

[root@svr5 ~]# sed -n '1p' /etc/rc.local
#!/bin/s

而在执行d删除等过滤操作时,希望看到的是删除符合条件的文本之后还能够被保留下来的文本,所以这时候就不应该使用 -n 选项了。比如,删除/etc/rc.local文件的第1-4行文本:

[root@svr5 ~]# sed '1,4d' /etc/rc.local
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

2)sed命令的 -i 选项
正常情况下,sed命令所做的处理只是把操作结果(包括打印、删除等)输出到当前终端屏幕,而并不会对原始文件做任何更改:

[root@svr5 ~]# cp /etc/rc.local rclocal.txt  	//复制为新文件,用作测试
[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.

touch /var/lock/subsys/local
[root@svr5 ~]# sed '1,4d' rclocal.txt  			//删除第1~4行,输出结果
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
[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.

touch /var/lock/subsys/local

若希望直接修改文件内容,应添加选项 -i 。
比如,直接删除rcloal.txt文件的第1~4行,不输出结果:

[root@svr5 ~]# sed -i '1,4d' rclocal.txt    		//删除操作
[root@svr5 ~]# cat rclocal.txt  					//确认删除结果
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

下文中关于使用sed修改文件的示例中,为了避免大家在练习过程中因误操作导致系统故障,部分命令省略 –i 选项,不再逐一说明。需要时,大家可自行加上此选项。
3)sed命令的 -e 选项
选项 -e 用来指定一条“编辑指令”,实际上 -e 通常是被省略了;当需要执行多条编辑指令时,可添加多组 -e 操作。比如,输入/etc/rc.local文件的第1行、第4行,可以采用这种方式:

[root@svr5 ~]# sed -n -e '1p' -e '4p' /etc/rc.local
#!/bin/sh
# You can put your own initialization stuff in here if you don't

如果不使用 -e ,也可以用分号来隔离多个操作(如果有定址条件,则应该使用{ }括起来),比如:

[root@svr5 ~]# sed -n '1p;4p' /etc/rc.local
#!/bin/sh
# You can put your own initialization stuff in here if you don't

或者:

[root@svr5 ~]# sed -n '{1p;4p}' /etc/rc.local
#!/bin/sh
# You can put your own initialization stuff in here if you don't

步骤二:认识sed工具的p输出操作
先创建一个练习用的测试文件,每一行之前添加行号,方便练习时查看效果:

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

1)输出所有行,相当于cat命令。

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

2)输出第4行。

[root@svr5 ~]# sed -n '4p' rclocal.txt
     4  # You can put your own initialization stuff in here if you don't

3)输出第4~7行。

[root@svr5 ~]# sed -n '4,7p' rclocal.txt
     4  # You can put your own initialization stuff in here if you don't
     5  # want to do the full Sys V style init stuff.
     6
     7  touch /var/lock/subsys/local

4)输出第4行和第7行。

[root@svr5 ~]# sed -n '4p;7p' rclocal.txt
     4  # You can put your own initialization stuff in here if you don't
     7  touch /var/lock/subsys/local

5)输出第2行及之后的3行。

[root@svr5 ~]# sed -n '2,+3p' rclocal.txt
     2  #
     3  # This script will be executed *after* all the other init scripts.
     4  # You can put your own initialization stuff in here if you don't
     5  # want to do the full Sys V style init stuff.

6)输出以local结尾的行。

[root@svr5 ~]# sed -n '/local$/p' rclocal.txt
     7  touch /var/lock/subsys/local

7)输出奇数行。

[root@svr5 ~]# sed -n 'p;n' rclocal.txt
     1  #!/bin/sh
     3  # This script will be executed *after* all the other init scripts.
     5  # want to do the full Sys V style init stuff.
     7  touch /var/lock/subsys/local

8)输出偶数行。

[root@svr5 ~]# sed -n 'n;p' rclocal.txt
     2  #
     4  # You can put your own initialization stuff in here if you don't
     6
9)从第5行输出到最后一行。
[root@svr5 ~]# sed -n '5,$p' rclocal.txt
     5  # want to do the full Sys V style init stuff.
     6
     7  touch /var/lock/subsys/local
10)输出文本的行数。
[root@svr5 ~]# sed -n '$=' rclocal.txt
7

步骤三:认识sed工具的d输出操作
还以rclocal.txt文件为例,文件内容如下所示:

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

1)删除第3~5行文本

[root@svr5 ~]# sed '3,5d' rclocal.txt
     1  #!/bin/sh
     2  #
     6
     7  touch /var/lock/subsys/local

2)删除所有包含“init”的行。

[root@svr5 ~]# sed '/init/d' rclocal.txt
     1  #!/bin/sh
     2  #
     6
     7  touch /var/lock/subsys/local

3)删除所有包含“init”的行、所有包含“bin”的行。

[root@svr5 ~]# sed '/init/d;/bin/d' rclocal.txt
     2  #
     6
     7  touch /var/lock/subsys/local

4)删除不包括“init”的行。

[root@svr5 ~]# sed '/init/!d' rclocal.txt
     3  # This script will be executed *after* all the other init scripts.
     4  # You can put your own initialization stuff in here if you don't
     5  # want to do the full Sys V style init stuff.

这个实际效果相当于只显示包含“init”的行:

[root@svr5 ~]# sed -n '/init/p' rclocal.txt
     3  # This script will be executed *after* all the other init scripts.
     4  # You can put your own initialization stuff in here if you don't
     5  # want to do the full Sys V style init stuff.

5)删除文件的最后一行。

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

6)删除文件中的空行
手动新建一个测试文件:

[root@svr5 ~]# vim blankline.txt
abc

def
hijklmn



hello world
I am here

end

删除所有空行:

[root@svr5 ~]# sed '/^$/d' blankline.txt
abc
def
hijklmn
hello world
I am here
end

清理重复空行,若连续两个空行只保留一个:

[root@svr5 ~]# sed '/^$/{n;/^$/d}' blankline.txt
abc

def
hijklmn


hello world
I am here

end

步骤四:认识sed工具的s替换操作
还以rclocal.txt文件为例,文件内容如下所示:

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

1)将所有行中的第一个“ll”(如果有的话)替换为“TARENA”。

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

2)将所有的“ll”(如果有的话)替换为“TARENA”。

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

3)将第3行内的第2个“script”替换为“SCRIPT”。

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

4)删除文件内指定的字符串(替换为空)。
删除所有的“init”字符串:

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

删除所有的“script”、所有的“stuff”、所有的字母e,或者的关系用转义方式 | 来表示:

[root@svr5 ~]# sed 's/script\|stuff\|e//g' rclocal.txt
     1  #!/bin/sh
     2  #
     3  # This  will b xcutd *aftr* all th othr init s.
     4  # You can put your own initialization  in hr if you don't
     5  # want to do th full Sys V styl init .
     6
     7  touch /var/lock/subsys/local

5)配置行的注释、解除注释。
以真实文件/etc/rc.local为例,文件内容如下:

[root@svr5 ~]# cat /etc/rc.local
#!/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.

touch /var/lock/subsys/local

解除/etc/rc.local文件第3~5行的注释(去掉开头的 # ):

[root@svr5 ~]# sed '3,5s/^#//' /etc/rc.local
#!/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.

touch /var/lock/subsys/local

将/etc/rc.local文件的第6~7行注释掉(行首添加 # ):

[root@svr5 ~]# sed '6,7s/^/#/' /etc/rc.local
#!/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.

#touch /var/lock/subsys/local
步骤五:利用sed完成本例要求的任务
先建立一个包含英文段落的测试文件,比如可使用/etc/nsswitch.conf文件。为了方便查看效果,我们将从这个文件中取第4~10行,并去掉开头的“# ”。开头的10行内容如下所示:

[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

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

[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个字符,第二次替换掉最后一个字符:

[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个单词,第二次替换掉最后一个单词:

[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”:

[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”:

[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)删除文件中所有的数字、行首的空格
因原文件内没有数字,行首也没有空格,这里稍作做一点处理,生成一个新测试文件:

[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文件为例,删除所有数字、行首空格的操作如下:

[root@svr5 ~]# sed -r 's/[0-9]//g;s/^( )+//' nssw2.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

6)为文件中每个大写字母添加括号
使用“&”可调用s替换操作中的整个查找串,所以可参考下列操作解决:

[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

或者:

[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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值