<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[实践OK]sed在查找到的某行下面添加一行的方法，文件行首添加几行:如何把一行文字添加的一个文件的头部，通过sed添加到/etc/hosts文件的第一行。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Sun, 26 Sep 2010 03:45:55 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	问题0：<br/>sed 在/etc/hosts文件的第一行添加两行：<br/>sed &#039;1i&#92;nameserver 10.70.63.150&#92;nnameserver 202.106.0.20&#039; /etc/resolv.conf&nbsp;&nbsp;&gt; /tmp/resolv.conf.tmp &amp;&amp; cat /tmp/resolv.conf.tmp &gt; /etc/resolv.conf<br/><textarea name="code" class="php" rows="15" cols="100">
sed &#039;1i&#92;nameserver 10.70.63.150&#92;nnameserver 202.106.0.20&#039; /etc/resolv.conf&nbsp;&nbsp;&gt; /tmp/resolv.conf.tmp &amp;&amp; cat /tmp/resolv.conf.tmp &gt; /etc/resolv.conf
</textarea><br/><br/>问题一：<br/>sed在查找到的某行下面添加一行的方法<br/>在文件nginx.conf下面加上一行新的vhost虚拟主机：<br/><textarea name="code" class="php" rows="15" cols="100">
&nbsp;&nbsp;&nbsp;&nbsp;include vhost/mini.conf;
&nbsp;&nbsp;&nbsp;&nbsp;include vhost/api.xiyou102.jackxiang.com.conf;
</textarea><br/><textarea name="code" class="php" rows="15" cols="100">
sed &#039;/mini.conf/a&#92;&nbsp;&nbsp;&nbsp;&nbsp;include vhost/api.xiyou102.jackxiang.com.conf;&#039; nginx.conf 
</textarea><br/><div class="code">cat hosts &#124; sed -e&#039;1ihello world!&#039; &gt; afteradd.txt</div><br/><br/>直接在某行进行sed替换：<br/>cat -n nginx.conf<br/>发现是92行：<br/>&nbsp;&nbsp;&nbsp;&nbsp;92&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include vhost/mini.conf;<br/>&nbsp;&nbsp;&nbsp;&nbsp;93&nbsp;&nbsp;&#125;<br/>于是：<br/><textarea name="code" class="php" rows="15" cols="100">
sed -e &#039;92a&#92;&nbsp;&nbsp;&nbsp;&nbsp;include vhost/api.xiyou102.jackxiang.com.conf;&#039; nginx.conf 
</textarea><br/>结果是在92行的下面插入了一行：<br/>&nbsp;&nbsp;&nbsp;&nbsp;include vhost/mini.conf;<br/>&nbsp;&nbsp;&nbsp;&nbsp;include vhost/api.xiyou102.jackxiang.com.conf;<br/>&#125;<br/>想真替换在sed -i ，加上-i即可。<br/><br/>上面的实践来自：<br/>原文件：<br/>[root@xiaowu shell]# cat -n file <br/>&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;aaaa<br/>&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp;&nbsp;bbbb<br/>&nbsp;&nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp;cccc<br/>&nbsp;&nbsp;&nbsp;&nbsp; 4&nbsp;&nbsp;dddd<br/><br/>现在要在第二行即“bbbb”行的下面添加一行，内容为“xiaowu”<br/>[root@xiaowu shell]# sed &#039;/bbbb/a&#92;xiaowu&#039; file <br/>aaaa<br/>bbbb<br/>xiaowu<br/>cccc<br/>dddd<br/><br/>如果要加两行“xiaowu”可以用一下语句，注意用“&#92;n”换行<br/>[root@xiaowu shell]# sed &#039;/bbbb/a&#92;xiaowu&#92;nxiaowu&#039; file <br/>aaaa<br/>bbbb<br/>xiaowu<br/>xiaowu<br/>cccc<br/>dddd<br/><br/>如果要在第二行即“bbbb”行的上添加一行，内容为“xiaowu”，可以把参数“a”换成“i”<br/>[root@xiaowu shell]# sed &#039;/b/i&#92;xiaowu&#039; file <br/>aaaa<br/>xiaowu<br/>bbbb<br/>cccc<br/>dddd<br/><br/>以上文件中只有一行匹配，如果文件中有两行或者多行匹配，结果有是如何呢？<br/><br/>[root@xiaowu shell]# cat -n file <br/>&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;aaaa<br/>&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp;&nbsp;bbbb<br/>&nbsp;&nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp;cccc<br/>&nbsp;&nbsp;&nbsp;&nbsp; 4&nbsp;&nbsp;bbbb<br/>&nbsp;&nbsp;&nbsp;&nbsp; 5&nbsp;&nbsp;dddd<br/><br/>[root@xiaowu shell]# sed &#039;/bbbb/a&#92;xiaowu&#039; file <br/>aaaa<br/>bbbb<br/>xiaowu<br/>cccc<br/>bbbb<br/>xiaowu<br/>dddd<br/><br/>由结果可知，每个匹配行的下一行都会被添加“xiaowu”<br/><br/>那么如果指向在第二个“bbbb”的下一行添加内容“xiaowu”，该如何操作呢？<br/>可以考虑先获取第二个“bbbb”行的行号，然后根据行号在此行的下一行添加“xiaowu”<br/><br/>获取第二个“bbbb”行的行号的方法：<br/>方法一：<br/>[root@xiaowu shell]# cat -n file &#124;grep b &#124;awk &#039;&#123;print $1&#125;&#039;&#124;sed -n &quot;2&quot;p<br/>4<br/>方法二：<br/>[root@xiaowu shell]# sed -n &#039;/bbbb/=&#039; file &#124;sed -n &quot;2&quot;p<br/>4<br/>由结果可知第二个“bbbb”行的行号为4，然后再在第四行的前或后添加相应的内容:<br/>[root@xiaowu shell]# sed -e &#039;4a&#92;xiaowu&#039; file <br/>aaaa<br/>bbbb<br/>cccc<br/>bbbb<br/>xiaowu<br/>dddd<br/>[root@xiaowu shell]# sed -e &#039;4a&#92;xiaowu&#92;nxiaowu&#039; file <br/>aaaa<br/>bbbb<br/>cccc<br/>bbbb<br/>xiaowu<br/>xiaowu<br/>dddd<br/><br/><br/>向指定行的末尾添加指定内容，比如在“ccccc”行的行尾介绍“ eeeee”<br/><br/>[root@xiaowu shell]# cat file<br/>aaaaa<br/>bbbbb<br/>ccccc<br/>ddddd<br/>[root@xiaowu shell]# sed &#039;s/cc.*/&amp; eeeee/g&#039; file<br/>aaaaa<br/>bbbbb<br/>ccccc eeeee<br/>ddddd<br/>=======================================================================<br/>问题二：<br/>文件行首添加几行:如何把一行文字添加的一个文件的头部，通过sed添加到第一行<br/>我认为最好的方法：<br/><div class="code">cat - file &lt;&lt;&lt; 123</div><br/><div class="code">cat - hosts &lt;&lt;&lt; 123 &gt; out.txt</div><br/><br/>如在sql前面加入一行：set names utf8,其见如下：<br/><div class="code">&#91;/home/jackxiang/sed&#93;# vi append.txt <br/>sql<br/>sql2<br/>sql3</div><br/><br/><div class="code">cat - append.txt &lt;&lt;&lt; &quot;set names utf8&quot;</div><br/><div class="code">&#91;/home/jackxiang/sed&#93;# cat - append.txt &lt;&lt;&lt; &quot;set names utf8&quot; &gt; addfirstline.txt</div><br/><div class="code">&#91;/home/jackxiang/sed&#93;# <br/>vi addfirstline.txt <br/>set names utf8<br/>sql<br/>sql2<br/>sql3<br/></div>
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]sed在查找到的某行下面添加一行的方法，文件行首添加几行:如何把一行文字添加的一个文件的头部，通过sed添加到/etc/hosts文件的第一行。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>