天天看点

linux下sed的使用(中)

    linux下sed的使用(中)

本篇主要讲解:

---sed文本块的处理

一、sed文本块的处理

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

 常用的处理选项有:

<code>i 行前插入文本(insert)</code>

<code>a 行后插入文本(append)</code>

<code>c 替换当前行(change)</code>

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

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

还是用测试文档:rclocal.txt

<code>[root@svr5 ~]</code><code># cat rclocal.txt</code>

<code>1 </code><code>#!/bin/sh</code>

<code>2 </code><code>#</code>

<code>3 </code><code># This script will be executed *after* all the other init scripts.</code>

<code>4 </code><code># You can put your own initialization stuff in here if you don't</code>

<code>5 </code><code># want to do the full Sys V style init stuff.</code>

<code>6</code>

<code>7 </code><code>touch</code> <code>/var/lock/subsys/local</code>

1)行前插入文本 i

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

<code>[root@svr5 ~]</code><code># sed '3iInsert before' rclocal.txt</code>

<code>Insert before</code>

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

<code>[root@svr5 ~]</code><code># sed '$iInsert before' rclocal.txt</code>

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

<code>[root@svr5 ~]</code><code># sed '/init/ixxxx' rclocal.txt</code>

<code>.xxxx</code>

<code>xxxx</code>

2)行后插入文本 a

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

<code>[root@svr5 ~]</code><code># sed '3aInsert after' rclocal.txt</code>

<code>Insert after</code>

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

<code>[root@svr5 ~]</code><code># sed '$aInsert after' rclocal.txt</code>

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

<code>[root@svr5 ~]</code><code># sed '/stuff/axxxx' rclocal.txt</code>

3)替换当前行 c

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

<code>[root@svr5 ~]</code><code># sed '1c#!/bin/bash' rclocal.txt</code>

<code>#!/bin/bash</code>

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

<code>[root@svr5 ~]</code><code># sed '1,4c#!/bin/bash' rclocal.txt</code>

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

<code>[root@svr5 ~]</code><code># sed '/\/bin\/sh/c#!/bin/bash' rclocal.txt</code>

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

<code>[root@svr5 ~]</code><code># sed '/\&lt;init\&gt;/c#!/bin/bash' rclocal.txt</code>

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

4)多行文本的处理

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

<code>[root@svr5 ~]</code><code># sed '3ixxxx\nyyyy\nzzzz' rclocal.txt         //方法1</code>

<code>yyyy</code>

<code>zzzz</code>

<code>[root@svr5 ~]</code><code># sed '3ixxxx\</code>

<code>&gt; yyyy\</code>

<code>&gt; zzzz' rclocal.txt                                         </code><code>//</code><code>方法2</code>

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

1)修改主机名

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

修改前:

<code>[root@svr5 ~]</code><code># cat /etc/sysconfig/network</code>

<code>NETWORKING=</code><code>yes</code>

<code>NETWORKING_IPV6=no</code>

<code>HOSTNAME=svr5.tarena.com</code>

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

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

确认替换结果:

<code>[root@svr5 ~]</code><code># cat /etc/sysconfig/network NETWORKING=yes</code>

<code>HOSTNAME=mysvr.example.org</code>

2)添加hosts主机映射记录

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

<code>[root@svr5 ~]</code><code># sed -i '$a192.168.4.5 svr5.example.com svr5\</code>

<code>&gt; 119.75.217.56 www.baidu.com' </code><code>/etc/hosts</code>

验证添加效果:

<code>[root@svr5 ~]</code><code># tail -2 /etc/hosts</code>

<code>192.168.4.5 svr5.example.com svr5</code>

<code>119.75.217.56 www.baidu.com</code>

2.sed文本处理练习

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

<code>[root@svr5 ~]</code><code># head -10 /etc/nsswitch.conf</code>

<code>#</code>

<code># /etc/nsswitch.conf</code>

<code># An example Name Service Switch config file. This file should be</code>

<code># sorted with the most-used services at the beginning.</code>

<code># The entry '[NOTFOUND=return]' means that the search for an</code>

<code># entry should stop if the search in the previous entry turned</code>

<code># up nothing. Note that if the search failed due to some other reason</code>

<code># (like no NIS server responding) then the search continues with the</code>

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

<code>[root@svr5 ~]</code><code># sed -n '4,10p' /etc/nsswitch.conf | sed 's/# //' &gt; nssw.txt</code>

<code>[root@svr5 ~]</code><code># cat nssw.txt</code>

<code>An example Name Service Switch config </code><code>file</code><code>. This </code><code>file</code> <code>should be</code>

<code>sorted with the most-used services at the beginning.</code>

<code>The entry </code><code>'[NOTFOUND=return]'</code> <code>means that the search </code><code>for</code> <code>an</code>

<code>entry should stop </code><code>if</code> <code>the search </code><code>in</code> <code>the previous entry turned</code>

<code>up nothing. Note that </code><code>if</code> <code>the search failed due to some other reason</code>

<code>(like no NIS server responding) </code><code>then</code> <code>the search continues with the</code>

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

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

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

<code>[root@svr5 ~]</code><code># sed 's/.//2;s/.$//' nssw.txt</code>

<code>A example Name Service Switch config </code><code>file</code><code>. This </code><code>file</code> <code>should b</code>

<code>srted with the most-used services at the beginning</code>

<code>Te entry </code><code>'[NOTFOUND=return]'</code> <code>means that the search </code><code>for</code> <code>a</code>

<code>etry should stop </code><code>if</code> <code>the search </code><code>in</code> <code>the previous entry turne</code>

<code>u nothing. Note that </code><code>if</code> <code>the search failed due to some other reaso</code>

<code>(ike no NIS server responding) </code><code>then</code> <code>the search continues with th</code>

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

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

<code>[root@svr5 ~]</code><code># sed -r 's/[a-Z]+//2;s/[a-Z]+([^a-Z]*)$/\1/' nssw.txt</code>

<code>An Name Service Switch config </code><code>file</code><code>. This </code><code>file</code> <code>should</code>

<code>sorted the most-used services at the .</code>

<code>The </code><code>'[NOTFOUND=return]'</code> <code>means that the search </code><code>for</code>

<code>entry stop </code><code>if</code> <code>the search </code><code>in</code> <code>the previous entry</code>

<code>up . Note that </code><code>if</code> <code>the search failed due to some other</code>

<code>(like NIS server responding) </code><code>then</code> <code>the search continues with</code>

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

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

<code>[root@svr5 ~]</code><code># sed -r 's/^(.)(.)(.*)/\2\1\3/' nssw.txt</code>

<code>nA example Name Service Switch config </code><code>file</code><code>. This </code><code>file</code> <code>should be</code>

<code>osrted with the most-used services at the beginning.</code>

<code>hTe entry </code><code>'[NOTFOUND=return]'</code> <code>means that the search </code><code>for</code> <code>an</code>

<code>netry should stop </code><code>if</code> <code>the search </code><code>in</code> <code>the previous entry turned</code>

<code>pu nothing. Note that </code><code>if</code> <code>the search failed due to some other reason</code>

<code>l(ike n up . Note that </code><code>if</code> <code>the search failed due to some other</code>

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

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

<code>[root@svr5 ~]</code><code># 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</code>

<code>with sorted the most-used services at the beginning.</code>

<code>entry The </code><code>'[NOTFOUND=return]'</code> <code>means that the search </code><code>for</code> <code>an</code>

<code>should entry stop </code><code>if</code> <code>the search </code><code>in</code> <code>the previous entry turned</code>

<code>nothing up. Note that </code><code>if</code> <code>the search failed due to some other reason</code>

<code>(no like NIS server responding) </code><code>then</code> <code>the search continues with the</code>

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

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

<code>[root@svr5 ~]</code><code># sed 's/o/o7/;s/l/l4/;3,5s/^/ /' nssw.txt &gt; nssw2.txt</code>

<code>[root@svr5 ~]</code><code># cat nssw2.txt</code>

<code>An exampl4e Name Service Switch co7nfig </code><code>file</code><code>. This </code><code>file</code> <code>should be</code>

<code>so7rted with the most-used services at the beginning.</code>

<code>.The entry </code><code>'[NOTFOUND=return]'</code> <code>means that the search fo7r an</code>

<code>entry sho7ul4d stop </code><code>if</code> <code>the search </code><code>in</code> <code>the previous entry turned</code>

<code>up no7thing. Note that </code><code>if</code> <code>the search fail4ed due to some other reason</code>

<code>(l4ike no7 NIS server responding) </code><code>then</code> <code>the search continues with the</code>

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

<code>1.[root@svr5 ~]</code><code># sed -r 's/[0-9]//g;s/^( )+//' nssw2.txt</code>

<code>2.An example Name Service Switch config </code><code>file</code><code>. This </code><code>file</code> <code>should be</code>

<code>3.sorted with the most-used services at the beginning.</code>

<code>4.</code><code>#</code>

<code>5.The entry </code><code>'[NOTFOUND=return]'</code> <code>means that the search </code><code>for</code> <code>an</code>

<code>6.entry should stop </code><code>if</code> <code>the search </code><code>in</code> <code>the previous entry turned</code>

<code>7.up nothing. Note that </code><code>if</code> <code>the search failed due to some other reason</code>

<code>8.(like no NIS server responding) </code><code>then</code> <code>the search continues with the</code>

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

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

<code>[root@svr5 ~]</code><code># sed 's/[A-Z]/(&amp;)/g' nssw.txt</code>

<code>(A)n example (N)ame (S)ervice (S)witch config </code><code>file</code><code>. (T)his </code><code>file</code> <code>should be</code>

<code>(T)he entry </code><code>'[(N)(O)(T)(F)(O)(U)(N)(D)=return]'</code> <code>means that the search </code><code>for</code> <code>an</code>

<code>up nothing. (N)ote that </code><code>if</code> <code>the search failed due to some other reason</code>

<code>(like no (N)(I)(S) server responding) </code><code>then</code> <code>the search continues with the</code>

或者:

<code>[root@svr5 ~]</code><code># sed -r 's/([A-Z])/(\1)/g' nssw.txt</code>

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

继续阅读