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

📄 ch34_04.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 34] 34.4 sed Addressing Basics </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:47:12Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch34_01.htm"TITLE="34. The sed Stream Editor"><LINKREL="prev"HREF="ch34_03.htm"TITLE="34.3 Testing and Using a sed Script: checksed, runsed "><LINKREL="next"HREF="ch34_05.htm"TITLE="34.5 Order of Commands in a Script "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch34_03.htm"TITLE="34.3 Testing and Using a sed Script: checksed, runsed "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.3 Testing and Using a sed Script: checksed, runsed "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 34<BR>The sed Stream Editor</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch34_05.htm"TITLE="34.5 Order of Commands in a Script "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.5 Order of Commands in a Script "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7977">34.4 sed Addressing Basics </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-7977-IX-SED-EDITOR-ADDRESSING"></A></P><PCLASS="para">A <EMCLASS="emphasis">sed</EM> command can specify zero, one, or two addresses.An address can be a line number, a line addressing symbol, or a <SPANCLASS="link">regular expression (<ACLASS="linkend"HREF="ch26_04.htm"TITLE="Using Metacharacters in Regular Expressions ">26.4</A>)</SPAN>that describes a pattern.</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">If no address is specified, then the command is applied to each line.</P></LI><LICLASS="listitem"><PCLASS="para">If there is only one address, the command is appliedto any line matching the address.</P></LI><LICLASS="listitem"><PCLASS="para">If two comma-separated addresses are specified, the command isperformed on the first matching line and all succeeding lines upto and including a line matching the second address.This range may match multiple times throughout the input.</P></LI><LICLASS="listitem"><PCLASS="para">If an address is followed by an exclamation mark (<CODECLASS="literal">!</CODE>), the commandis applied to all lines that do <EMCLASS="emphasis">not</EM> match the address.</P></LI></UL><PCLASS="para">To illustrate how addressing works, let's look at examplesusing the delete command, <EMCLASS="emphasis">d</EM>.A script consisting of simply the <EMCLASS="emphasis">d</EM> command and no address:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">d</PRE></BLOCKQUOTE></P><PCLASS="para">produces no output since itdeletes <EMCLASS="emphasis">all</EM> lines.</P><PCLASS="para">When a line number is supplied as an address, the commandaffects only that line.For instance, the followingexample deletes only the first line:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">1d</PRE></BLOCKQUOTE></P><PCLASS="para">&#13;The line number refers to an internal line count maintainedby <EMCLASS="emphasis">sed</EM>.This counter is not reset for multiple input files.Thus, no matter how many files were specified as input, there is onlyone line 1 in the input stream.</P><PCLASS="para">Similarly, the input stream has only one last line.Itcan be specified using the addressing symbol, <CODECLASS="literal">$</CODE>.The followingexample deletes the last line of input:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$d</PRE></BLOCKQUOTE></P><PCLASS="para">The <CODECLASS="literal">$</CODE> symbol should not be confused with the <CODECLASS="literal">$</CODE> usedin regular expressions, where it means the end of the line.</P><PCLASS="para">When a regular expression is supplied as an address, the commandaffects only the lines matching that pattern.The regular expression must be enclosed by slashes (<CODECLASS="literal">/</CODE>).The following delete command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/^$/d</PRE></BLOCKQUOTE></P><PCLASS="para">deletes only blank lines.All other lines are passed throughuntouched.</P><PCLASS="para">If you supply two addresses, then you specifya range of lines over which the command is executed.The following example shows how to delete all linessurrounded by a pair ofmacros, in this case, .TS and .TE, that mark a table as<SPANCLASS="link"><EMCLASS="emphasis">tbl</EM> (<ACLASS="linkend"HREF="ch43_15.htm"TITLE="From a Source File to the Printer ">43.15</A>)</SPAN>input:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/^\.TS/,/^\.TE/d</PRE></BLOCKQUOTE></P><PCLASS="para">It deletes all lines beginning with the line matched by the first pattern uptoand including the line matched by the second pattern.Lines outside this range are not affected.If there is more than one table (another .TS/.TE pair after thefirst), those tables will also be deleted.</P><PCLASS="para">The following command deletes from line 50 to the last line in the file:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">50,$d</PRE></BLOCKQUOTE></P><PCLASS="para">You can mix a line address and a pattern address:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">1,/^$/d</PRE></BLOCKQUOTE></P><PCLASS="para">This example deletes from the first line up to the first blank line,which, for instance, will delete a mail header from a<SPANCLASS="link">mail message (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>thatyou have saved in a file.</P><PCLASS="para">You can think of the first address as enabling the actionand the second address as disabling it.<EMCLASS="emphasis">sed</EM> has no way of looking ahead to determine if the second matchwill be made.The action will be applied to linesonce the first match is made.The command will be applied to <EMCLASS="emphasis">all</EM> subsequent lines untilthe second match ismade.In the previous example, if the filedid not contain a blank line, then all lines would be deleted.</P><PCLASS="para">An exclamation mark following an addressreverses the sense of the match.For instance,the following script deletes all lines <EMCLASS="emphasis">except</EM> those inside<EMCLASS="emphasis">tbl</EM> input:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/^\.TS/,/^\.TE/!d</PRE></BLOCKQUOTE></P><PCLASS="para">&#13;This script, in effect, extracts <EMCLASS="emphasis">tbl</EM> input from a source file.(This can be handy for testing the format of tables.)</P><PCLASS="para">Curly braces (<CODECLASS="literal">{</CODE><CODECLASS="literal">}</CODE>)let you give more than one command with an address.For example, to search every line of a table, capitalize the word<CODECLASS="literal">Caution</CODE> on any of those lines, and delete any line with<CODECLASS="literal">.sp&nbsp;2p</CODE>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/^\.TS/,/^\.TE/{    s/Caution/CAUTION/g    /^\.sp 2p/d}</PRE></BLOCKQUOTE></P><ACLASS="indexterm"NAME="AUTOID-38487"></A><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">DD</SPAN> <SPANCLASS="bibliomisc">from O'Reilly &amp; Associates' <CITECLASS="citetitle">sed &amp; awk</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_03.htm"TITLE="34.3 Testing and Using a sed Script: checksed, runsed "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.3 Testing and Using a sed Script: checksed, runsed "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="SECT1"HREF="ch34_05.htm"TITLE="34.5 Order of Commands in a Script "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.5 Order of Commands in a Script "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">34.3 Testing and Using a sed Script: checksed, runsed </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">34.5 Order of Commands in a Script </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 + -