📄 472.html
字号:
<br>
SED 手册 - 4.介绍函数参数 <br>
http://phi.sinica.edu.tw aspac@phi.sinica.edu.tw (2001-07-30 07:00:00) <br>
<br>
本章将以一节一个函数参数的方式 ,介绍所有 sed 提供的函数参数 , 其中有 <br>
<br>
<br>
| s | d | a | i | c | p | l | r | w | y | ! | n | q | = | # | N | D | P | h | H | g | G | x | b | t | <br>
<br>
<br>
另外 , 在各节中 , 首先简单介绍函数参数功能 , 接着说明函数参数与位址参数配合的格式 , 而其中也一描述 <br>
sed 执行此函数参数的工作情形。 <br>
4.1 s <br>
函数参数 s 表示替换(substitute)文件内字串。其指令格式如下 : <br>
[address1[ ,address2]] s/pattern/replacemen/[flag] <br>
对上述格式有下面几点说明 : <br>
<br>
函数参数 s 最多与两个位址参数配合。 <br>
关於 "s/pattern/replacement/[flag]"(解[12]) 有下面几点说明: <br>
pattern : 它为 reguler expression 字串。它表示文件中要被替换的字串。 <br>
replacement : 它为一般字串。但其内出现下列字元有特别意义 : <br>
<br>
& : 代表其前 pattern 字串。例如 <br>
sed -e 's/test/& my car/' 资料档名 <br>
<br>
指令中 , & 代表 pattern 字串 "test"。故执行後 , 资料档的 "test" 被替换成 "test my car"。 <br>
: 代表 pattern 中被第 n 个 ( 、)(参照[附录 A]) 所括起来的字串。例如 <br>
sed -e 's/(test) (my) (car)/[2 3 1]/' 资料档名 <br>
<br>
指令中 , 1 表示 "test"、2 表示 "my"、1 表示 "car" 字串。故执行後 , 资料档的 "test my car" 被替换 <br>
成 "[my car test]"。 <br>
: 可用它来还原一些特殊符号(如上述的 & 与 )本身字面上的意义 , 或用它来代表换行。 <br>
flag : 主要用它来控制一些替换情况 : <br>
当 flag 为 g 时 , 代表替换所有符合(match)的字串 。 <br>
当 flag 为十进位数字 m 时 , 代表替换行内第 m 个符合的字串。 <br>
当 flag 为 p 时 , 代表替换第一个符合 pattern 的字串後 , 将资料输出标准输出档。 <br>
当 flag 为 w wfile 时 , 代表替换第一个符合 pattern 的字串後 , 输出到 wfile 档内(如果 wfile 不存在 , 则会 <br>
重新开启名为 wfile 的档案)。 <br>
当没有 flag 时 , 则将资料行内第一个符合 pattern 的字串以 replacement 字串来替换 。 <br>
delimiter : 在 "/pattern/replace/[flag] " 中 "/" 被当成一 delimiter。除了空白(blank)、换行(newline) 之外 , <br>
使用者可用任何字元作为 delimiter。例如下述编辑指令 <br>
s#/usr#/usr1#g <br>
<br>
上述命令中 verb|#| 为 delimiter。如果用 "/" 做 delimiter , 则 sed 会将 pattern 与 replacement 中的 "/" <br>
当成 delimiter 而发生错误。 <br>
例: <br>
题目 : 替换 input.dat 档(後面如果没有特别指定 , 均假设文件档名为 input.dat)内 "1996" 字串成 "1997" , 同时 <br>
将这些资料行存入 year97.dat 档内。 <br>
说明 : 用函数参数 s 指示 sed 将 "1996" 字串替换成 "1997" , 另外用 s argument 中的 flag w 指示 sed 将替换 <br>
过的资料行存入 year97.dat 档内。 <br>
sed 命令列: <br>
sed -e 's/1996/1997/w year97.dat' input.dat <br>
<br>
<br>
4.2 d <br>
函数参数 d 表示删除资料行 , 其指令格式如下: <br>
<br>
[address1[ ,address2]] d <br>
<br>
<br>
对上述格式有下面几点说明: <br>
<br>
函数参数 d 最多与两个位址参数配合。 <br>
sed 执行删除动作情况如下 : <br>
将 pattern space 内符合位址参数的资料删除。 <br>
将下一笔资料读进 pattern space 。 <br>
重新执行 sed script。 <br>
例 : 可参考 section 3.3。 <br>
4.3 a <br>
函数参数 a 表示将资料添加到文件中。其指令格式如下: <br>
<br>
[address1] a 使用者所输入的资料 <br>
<br>
对上述格式有下面几点说明: <br>
<br>
<br>
函数参数 a 最多与一个位址参数配合。 <br>
函数参数 a 紧接着 "" 字元用来表示此行结束 , 使用者所输入的资料必须从下一行输入。如果资料超过一行 , 则须在 <br>
每行的结尾加入""。 <br>
sed 执行添加动作情况如下 : 当 pattern space 内资料输出後 , sed 跟着输出使用者所输入的资料。 <br>
例 : <br>
题目: 添加 "多工作业系统" 在含 "UNIX" 字串的资料行後。假设 input.dat 档的内容如下 : <br>
UNIX <br>
<br>
说明: 用函数参数 a 将所输入的资料添加在含 "UNIX" 字串的资料行後。 <br>
sed 命令列如下 : <br>
sed -e '/UNIX/a <br>
多工作业系统 <br>
' input.dat <br>
<br>
执行上述命令後 , 其输出结果如下 : <br>
UNIX <br>
多工作业系统 <br>
<br>
4.4 i <br>
函数参数 i 表示将资料插入文件中。其指令格式如下: <br>
[address1] i 使用者所输入的资料 <br>
<br>
<br>
对上述格式有下面几点说明: <br>
<br>
函数参数 i 最多与一个位址参数配合。 <br>
函数参数 i 紧接着 "" 字元用来表示此行结束 , 使用者所输入的资料必须从下一行输入。如果资料超过一行 , 则须在 <br>
每行的结尾加入""。 <br>
sed 执行插入动作的情况如下 : 在 pattern space 内资料输出前 , sed 先输出使用者所输入的资料。 <br>
例 : <br>
题目: 将 "文章版权属於中央研究院" 插在 input.dat 档中含 "院长 : 李远哲" 的资料行之前。假设 input.dat 档内 <br>
容如下 : <br>
院长 : 李远哲 <br>
<br>
说明: 用函数参数 i 将资料行 "文章版权属於中央研究院" 插在含 "院长 : 李远哲" 的资料行之前。 <br>
sed 命令列如下: <br>
sed -e '/院长 : 李远哲/i <br>
文章版权属於中央研究院 <br>
' input.dat <br>
<br>
执行上述命令後的输出如下 : <br>
文章版权属於中央研究院 <br>
院长 : 李远哲 <br>
<br>
4.5 c <br>
函数参数 c 表示改变文件中的资料。其格式如下: <br>
[address1[ ,address2]]c 使用者所输入的资料 <br>
<br>
<br>
对上述格式有下面几点说明: <br>
<br>
函数参数 c 最多与两个位址参数配合。 <br>
函数参数 c 紧接着 "" 字元用来表示此行结束 , 使用者所输入的资料必须从下一行输入。如果资料超过一行 , 则须在 <br>
每行的结尾加入""。 <br>
sed 执行改变动作的情况 : 在 pattern space 内资料输出时 , sed 改变它成为使用者所输入的资料。 <br>
例 : 参考 section 3.1 之例二、三。 <br>
4.6 p <br>
函数参数 p 表示印出资料。其指令格式如下 : <br>
[address1[ , address2]] p <br>
<br>
<br>
对於上述格式有下面几点说明 : <br>
<br>
函数参数 p 最多与两个位址参数配合。 <br>
sed 执行印出动作的情况如下 : sed 拷备一份 pattern space 内容至标准输出档。 <br>
例 : 参考 section 3.4 开头的内容。 <br>
4.7 l <br>
函数参数 l , 除可将资料中的 nonprinting character 以 ASCII码列出外 , 其於均与函数参数 p 相同。例如 , 将下 <br>
面 input.dat 档中的 ^[ 以 ASCII 码印出 <br>
<br>
The Great ^[ is a movie starring Steve McQueen. <br>
<br>
执行命令 sed -e 'l' input.dat 後 , 则输出结果如下 : <br>
The Great </FONT><br>
</TD>
</TR>
<TR>
<TD colSpan=2><FONT
class=middlefont></FONT><BR>
<FONT
class=normalfont>全文结束</FONT> </TD>
</TR>
<TR>
<TD background="images/dot.gif" tppabs="http://www.linuxhero.com/docs/images/dot.gif" colSpan=2
height=10></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></DIV></TD>
<TD vAlign=top width="20%"
background="images/line.gif" tppabs="http://www.linuxhero.com/docs/images/line.gif" rowSpan=2>
<DIV align=center>
<table class=tableoutline cellspacing=1 cellpadding=4
width="100%" align=center border=0>
<tr class=firstalt>
<td noWrap background="images/bgline.gif" tppabs="http://www.linuxhero.com/docs/images/bgline.gif" colspan=2 height=21>
<font class=normalfont><b>所有分类</b></font></td>
</tr>
<tr class=secondalt> <td noWrap width=27%> <font class=normalfont>1:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type1.html" tppabs="http://www.linuxhero.com/docs/type1.html">非技术类</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>2:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type2.html" tppabs="http://www.linuxhero.com/docs/type2.html">基础知识</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>3:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type3.html" tppabs="http://www.linuxhero.com/docs/type3.html">指令大全</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>4:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type4.html" tppabs="http://www.linuxhero.com/docs/type4.html">shell</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>5:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type5.html" tppabs="http://www.linuxhero.com/docs/type5.html">安装启动</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>6:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type6.html" tppabs="http://www.linuxhero.com/docs/type6.html">xwindow</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>7:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type7.html" tppabs="http://www.linuxhero.com/docs/type7.html">kde</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>8:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type8.html" tppabs="http://www.linuxhero.com/docs/type8.html">gnome</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>9:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type9.html" tppabs="http://www.linuxhero.com/docs/type9.html">输入法类</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>10:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type10.html" tppabs="http://www.linuxhero.com/docs/type10.html">美化汉化</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>11:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type11.html" tppabs="http://www.linuxhero.com/docs/type11.html">网络配置</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>12:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type12.html" tppabs="http://www.linuxhero.com/docs/type12.html">存储备份</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>13:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type13.html" tppabs="http://www.linuxhero.com/docs/type13.html">杂项工具</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>14:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type14.html" tppabs="http://www.linuxhero.com/docs/type14.html">编程技术</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>15:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type15.html" tppabs="http://www.linuxhero.com/docs/type15.html">网络安全</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>16:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type16.html" tppabs="http://www.linuxhero.com/docs/type16.html">内核技术</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>17:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type17.html" tppabs="http://www.linuxhero.com/docs/type17.html">速度优化</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>18:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type18.html" tppabs="http://www.linuxhero.com/docs/type18.html">apache</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>19:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type19.html" tppabs="http://www.linuxhero.com/docs/type19.html">email</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>20:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type20.html" tppabs="http://www.linuxhero.com/docs/type20.html">ftp服务</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>21:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type21.html" tppabs="http://www.linuxhero.com/docs/type21.html">cvs服务</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>22:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type22.html" tppabs="http://www.linuxhero.com/docs/type22.html">代理服务</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>23:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type23.html" tppabs="http://www.linuxhero.com/docs/type23.html">samba</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>24:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type24.html" tppabs="http://www.linuxhero.com/docs/type24.html">域名服务</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>25:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type25.html" tppabs="http://www.linuxhero.com/docs/type25.html">网络过滤</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>26:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type26.html" tppabs="http://www.linuxhero.com/docs/type26.html">其他服务</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>27:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type27.html" tppabs="http://www.linuxhero.com/docs/type27.html">nfs</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>28:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type28.html" tppabs="http://www.linuxhero.com/docs/type28.html">oracle</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>29:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type29.html" tppabs="http://www.linuxhero.com/docs/type29.html">dhcp</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>30:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type30.html" tppabs="http://www.linuxhero.com/docs/type30.html">mysql</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>31:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type31.html" tppabs="http://www.linuxhero.com/docs/type31.html">php</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>32:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type32.html" tppabs="http://www.linuxhero.com/docs/type32.html">ldap</a></font></td> </tr> </table></td></tr> </table>
</DIV></TD></TR>
<TR vAlign=top>
<TD width="80%">
<DIV align=center><BR>
</DIV>
</TD></TR></TBODY></TABLE></TD></TR>
</TABLE></TD></TR>
</TABLE>
<TABLE cellSpacing=0 cellPadding=4 width="100%" bgColor=#eeeeee
border=0><TBODY>
<TR>
<TD width="50%">
<P><FONT class=middlefont>版权所有 © 2004 <A
href="mailto:bjchenxu@sina.com">linux知识宝库</A><BR>
违者必究. </FONT></P>
</TD>
<TD width="50%">
<DIV align=right><FONT class=middlefont>Powered by: <A
href="mailto:bjchenxu@sina.com">Linux知识宝库</A> Version 0.9.0 </FONT></DIV>
</TD></TR></TBODY></TABLE>
<CENTER></CENTER></TD></TR>
</TABLE></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -