⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch34_24.htm

📁 the unix power tools
💻 HTM
📖 第 1 页 / 共 3 页
字号:
>N</EM> command. Same as <EMCLASS="emphasis">p</EM> if <EMCLASS="emphasis">N</EM> has not been applied to a line.</P><PCLASS="para"><EMCLASS="emphasis">Example</EM></P><PCLASS="para">The following script prints each line containing <CODECLASS="replaceable"><I>word</I></CODE>and also the line before it:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">N/<CODECLASS="replaceable"><I>word</I></CODE>/PD</PRE></BLOCKQUOTE></P><PCLASS="para"></P></DD><DTCLASS="term"><CODECLASS="literal">q</CODE></DT><DDCLASS="listitem"><PCLASS="para">[<CODECLASS="replaceable"><I>address</I></CODE>]<CODECLASS="literal">q</CODE><ACLASS="indexterm"NAME="AUTOID-39854"></A></P><PCLASS="para">Quit when <CODECLASS="replaceable"><I>address</I></CODE> is encountered. The addressed line is firstwritten to output (if default output is not suppressed),along with any text appended to it by previous <EMCLASS="emphasis">a</EM> or <EMCLASS="emphasis">r</EM> commands.See articles <ACLASS="xref"HREF="ch34_21.htm"TITLE="Uses of the sed Quit Command ">34.21</A> and<ACLASS="xref"HREF="ch34_22.htm"TITLE="Dangers of the sed Quit Command ">34.22</A>.</P><PCLASS="para"><EMCLASS="emphasis">Example</EM></P><PCLASS="para">Delete everything after the addressed line:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/Garbled text follows:/q</PRE></BLOCKQUOTE></P><PCLASS="para">&#13;Print only the first 50 lines of a file:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">50q</PRE></BLOCKQUOTE></P></DD><DTCLASS="term"><CODECLASS="literal">r</CODE></DT><DDCLASS="listitem"><PCLASS="para">[<CODECLASS="replaceable"><I>address</I></CODE>]<CODECLASS="literal">r </CODE><CODECLASS="replaceable"><I>file</I></CODE></P><PCLASS="para">Read contents of <CODECLASS="replaceable"><I>file</I></CODE> and append after the contents of thepattern space.Exactly one space must be put between the <CODECLASS="literal">r</CODE> and <CODECLASS="replaceable"><I>file</I></CODE>.</P><PCLASS="para"><EMCLASS="emphasis">Example</EM></P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/The list of items follows:/r item_file</PRE></BLOCKQUOTE></P></DD><DTCLASS="term"><CODECLASS="literal">s</CODE></DT><DDCLASS="listitem"><PCLASS="para">[<CODECLASS="replaceable"><I>address1</I></CODE>][<CODECLASS="literal">,</CODE><CODECLASS="replaceable"><I>address2</I></CODE>]<CODECLASS="literal">s/</CODE><CODECLASS="replaceable"><I>pattern</I></CODE><CODECLASS="literal">/</CODE><CODECLASS="replaceable"><I>replacement</I></CODE><CODECLASS="literal">/</CODE>[<CODECLASS="replaceable"><I>flags</I></CODE>]</P><PCLASS="para">Substitute <CODECLASS="replaceable"><I>replacement</I></CODE> for <CODECLASS="replaceable"><I>pattern</I></CODE> on each addressed line.If pattern addresses are used, the pattern <CODECLASS="literal">//</CODE> represents the lastpattern address specified.The following flags can be specified:<DLCLASS="variablelist"><DTCLASS="term"><CODECLASS="replaceable"><I>n</I></CODE></DT><DDCLASS="listitem"><PCLASS="para">Replace <CODECLASS="replaceable"><I>n</I></CODE>th instance of <CODECLASS="literal">/</CODE><CODECLASS="replaceable"><I>pattern</I></CODE><CODECLASS="literal">/</CODE> on each addressed line.<CODECLASS="replaceable"><I>n</I></CODE> is any number in the range 1 to 512 (default is 1).See article<ACLASS="xref"HREF="ch34_11.htm"TITLE="Search &amp; Replacement: One Match Among Many ">34.11</A>.</P></DD><DTCLASS="term"><CODECLASS="literal">g</CODE></DT><DDCLASS="listitem"><PCLASS="para">Replace all instances of <CODECLASS="literal">/</CODE><CODECLASS="replaceable"><I>pattern</I></CODE><CODECLASS="literal">/</CODE> on each addressed line, notjust the first instance.</P></DD><DTCLASS="term"><CODECLASS="literal">p</CODE></DT><DDCLASS="listitem"><PCLASS="para">Print the line if a successful substitution is done. If severalsuccessful substitutions are done, multiple copies of the line will beprinted.Often used in scripts with the<SPANCLASS="link"><EMCLASS="emphasis">-n</EM> command-line option (<ACLASS="linkend"HREF="ch34_02.htm"TITLE="Invoking sed ">34.2</A>)</SPAN>.</P></DD><DTCLASS="term"><CODECLASS="literal">w&nbsp;</CODE><CODECLASS="replaceable"><I>file</I></CODE></DT><DDCLASS="listitem"><PCLASS="para">Write the line to a <CODECLASS="replaceable"><I>file</I></CODE> if a replacement was done. A maximumof ten different files can be opened in a script.See articles <ACLASS="xref"HREF="ch34_07.htm"TITLE="Delimiting a Regular Expression ">34.7</A> through <ACLASS="xref"HREF="ch34_10.htm"TITLE="Referencing Portions of a Search String ">34.10</A>.</P><PCLASS="para"><EMCLASS="emphasis">Examples</EM></P><PCLASS="para">Here are some short, commented scripts:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"># Change third and fourth quote to ( and ):/function/{    s/&quot;/)/4    s/&quot;/(/3}# Remove all quotes on a given line:/Title/s/&quot;//g# Remove first colon or all quotes; print resulting lines:s/://ps/&quot;//gp# Change first &quot;if&quot; but leave &quot;ifdef&quot; alone:/ifdef/!s/if/   if/</PRE></BLOCKQUOTE></P><PCLASS="para"></P></DD></DL></P></DD><DTCLASS="term"><CODECLASS="literal">t</CODE></DT><DDCLASS="listitem"><PCLASS="para">[<CODECLASS="replaceable"><I>address1</I></CODE>][<CODECLASS="literal">,</CODE><CODECLASS="replaceable"><I>address2</I></CODE>]<CODECLASS="literal">t </CODE>[<CODECLASS="replaceable"><I>label</I></CODE>]</P><PCLASS="para">Test if any substitutions have been made on addressed lines,and if so, branch to line marked by <CODECLASS="literal">:</CODE><CODECLASS="replaceable"><I>label</I></CODE>.(See <EMCLASS="emphasis">b</EM> and <CODECLASS="literal">:</CODE>.) If<CODECLASS="replaceable"><I>label</I></CODE> is not specified, control falls through to bottom of script.See article<ACLASS="xref"HREF="ch34_20.htm"TITLE="The sed Test Command ">34.20</A>.</P><PCLASS="para">The <EMCLASS="emphasis">t</EM> command can be used like a<SPANCLASS="link"><EMCLASS="emphasis">case</EM> statement (<ACLASS="linkend"HREF="ch44_05.htm"TITLE="Test String Values with Bourne Shell case ">44.5</A>)</SPAN>in the Bourne shell.You test each case: when it's true, you exit the construct.</P><PCLASS="para"><EMCLASS="emphasis">Example</EM></P><PCLASS="para">Suppose you want to fill empty fields of a database. You have this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">ID: 1   Name: greg   Rate: 45ID: 2   Name: daleID: 3</PRE></BLOCKQUOTE></P><PCLASS="para">You want this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">ID: 1   Name: greg   Rate: 45   Phone: ??ID: 2   Name: dale   Rate: ??   Phone: ??ID: 3   Name: ????   Rate: ??   Phone: ??</PRE></BLOCKQUOTE></P><PCLASS="para">You need to test the number of fields already there.Here's the script (fields are tab-separated):</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/ID/{    s/ID: .* Name: .* Rate: .*/&amp;   Phone: ??/p    t    s/ID: .* Name: .*/&amp;   Rate: ??   Phone: ??/p    t    s/ID: .*/&amp;   Name: ??     Rate: ??   Phone: ??/p}</PRE></BLOCKQUOTE></P><PCLASS="para"></P></DD><DTCLASS="term"><CODECLASS="literal">w</CODE></DT><DDCLASS="listitem"><PCLASS="para">[<CODECLASS="replaceable"><I>address1</I></CODE>][<CODECLASS="literal">,</CODE><CODECLASS="replaceable"><I>address2</I></CODE>]<CODECLASS="literal">w </CODE><CODECLASS="replaceable"><I>file</I></CODE></P><PCLASS="para">Append, contents of pattern space to <CODECLASS="replaceable"><I>file</I></CODE>. This action occurs when the command is encountered rather than when the pattern space isoutput. Exactly one spacemust separate the <CODECLASS="literal">w</CODE> and <CODECLASS="replaceable"><I>file</I></CODE>.A maximum of ten different files can be opened in a script.This command will create the file if it does not exist; if the fileexists, its contents will be overwritten each time the scriptis executed. Multiple write commands that direct output to thesame file append to the end of the file.</P><PCLASS="para"><EMCLASS="emphasis">Example</EM></P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"># Store tbl and eqn blocks in a file:/^\.TS/,/^\.TE/w troff_stuff/^\.EQ/,/^\.EN/w troff_stuff</PRE></BLOCKQUOTE></P><PCLASS="para"></P></DD><DTCLASS="term"><CODECLASS="literal">x</CODE></DT><DDCLASS="listitem"><PCLASS="para">[<CODECLASS="replaceable"><I>address1</I></CODE>][<CODECLASS="literal">,</CODE><CODECLASS="replaceable"><I>address2</I></CODE>]<CODECLASS="literal">x</CODE></P><PCLASS="para">Exchange contents of the pattern space with the contents of thehold space.For examples, see <EMCLASS="emphasis">h</EM> and articles<ACLASS="xref"HREF="ch34_13.htm"TITLE="Hold Space: The Set-Aside Buffer ">34.13</A>and<ACLASS="xref"HREF="ch34_16.htm"TITLE="The Deliberate Scrivener ">34.16</A>.&#13;</P></DD><DTCLASS="term"><CODECLASS="literal">y</CODE></DT><DDCLASS="listitem"><PCLASS="para">[<CODECLASS="replaceable"><I>address1</I></CODE>][<CODECLASS="literal">,</CODE><CODECLASS="replaceable"><I>address2</I></CODE>]<CODECLASS="literal">y/</CODE><CODECLASS="replaceable"><I>abc</I></CODE><CODECLASS="literal">/</CODE><CODECLASS="replaceable"><I>xyz</I></CODE><CODECLASS="literal">/</CODE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-40026"></A>Translate characters. Change every instance of <CODECLASS="replaceable"><I>a</I></CODE>to <CODECLASS="replaceable"><I>x</I></CODE>, <CODECLASS="replaceable"><I>b</I></CODE> to <CODECLASS="replaceable"><I>y</I></CODE>, <CODECLASS="replaceable"><I>c</I></CODE> to <CODECLASS="replaceable"><I>z</I></CODE>, etc.See articles<ACLASS="xref"HREF="ch34_12.htm"TITLE="Transformations on Text ">34.12</A>and<ACLASS="xref"HREF="ch34_14.htm"TITLE="Transforming Part of a Line ">34.14</A>.</P><PCLASS="para"><EMCLASS="emphasis">Example</EM></P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"># Change item 1, 2, 3 to Item A, B, C .../^item [1-9]/y/i123456789/IABCDEFGHI/</PRE></BLOCKQUOTE></P></DD></DL><ACLASS="indexterm"NAME="AUTOID-40041"></A><ACLASS="indexterm"NAME="AUTOID-40042"></A><ACLASS="indexterm"NAME="AUTOID-40046"></A></DIV><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">DG</SPAN> <SPANCLASS="bibliomisc">from O'Reilly &amp; Associates' <CITECLASS="citetitle">UNIX in a Nutshell (SVR4/Solaris)</CITE></SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch34_23.htm"TITLE="34.23 sed Newlines, Quoting, and Backslashes in a Shell Script"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.23 sed Newlines, Quoting, and Backslashes in a Shell Script"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="CHAPTER"HREF="ch35_01.htm"TITLE="35. You Can't Quite Call This Editing"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 35. You Can't Quite Call This Editing"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">34.23 sed Newlines, Quoting, and Backslashes in a Shell Script</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">35. You Can't Quite Call This Editing</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed &amp; awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -