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

📄 ch34_18.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 34] 34.18 Multiline Delete </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:30Z"><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_17.htm"TITLE="34.17 Searching for Patterns Split Across Lines "><LINKREL="next"HREF="ch34_19.htm"TITLE="34.19 Making Edits Everywhere Except... "></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_17.htm"TITLE="34.17 Searching for Patterns Split Across Lines "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.17 Searching for Patterns Split Across Lines "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_19.htm"TITLE="34.19 Making Edits Everywhere Except... "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.19 Making Edits Everywhere Except... "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-5760">34.18 Multiline Delete </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-39168"></A>The <EMCLASS="emphasis">sed</EM> delete command, <EMCLASS="emphasis">d</EM>,deletes the contents of the<SPANCLASS="link">pattern space (<ACLASS="linkend"HREF="ch34_13.htm"TITLE="Hold Space: The Set-Aside Buffer ">34.13</A>)</SPAN>and causes a new lineof input to be read, with editing resuming at the top of the script. The Delete command, <EMCLASS="emphasis">D</EM>, works slightly differently: it deletes a portionof the pattern space, up to the first embedded newline. It doesnot cause a new line of input to be read; instead, it returnsto the top of the script, applying these instructions to whatremains in the pattern space.We can see the difference by writinga script that looksfor a series of blank lines and outputs a single blank line.The version below uses the delete command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"># reduce multiple blank lines to one; version using d command/^$/{   N   /^\n$/d}</PRE></BLOCKQUOTE></P><PCLASS="para">When a blank line is encountered, the next line is appendedto the pattern space. Then we try to match the embeddednewline. Note that the positional metacharacters, <CODECLASS="literal">^</CODE> and <CODECLASS="literal">$</CODE>,match the beginning and the end of the pattern space,respectively. Here's a test file: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">This line is followed by 1 blank line.This line is followed by 2 blank lines.This line is followed by 3 blank lines.This line is followed by 4 blank lines.This is the end.</PRE></BLOCKQUOTE></P><PCLASS="para">Running the script on the test file produces the following result:&#13;</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>sed -f sed.blank test.blank</B></CODE>This line is followed by 1 blank line.This line is followed by 2 blank lines.This line is followed by 3 blank lines.This line is followed by 4 blank lines.This is the end.</PRE></BLOCKQUOTE></P><PCLASS="para">Where there was an even numberof blank lines, all the blank lines were removed. Onlywhen there was an odd number was a single blank line preserved. That is becausethe delete command clears the entire pattern space. Once the firstblank line is encountered, the next line is read in, and both are deleted. If a third blank line isencountered, and the next line is not blank, the delete commandis not applied, and thus a blank line is output.If we use the multiline Delete command, we get a different result,and the one that we wanted:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/^\n$/D</PRE></BLOCKQUOTE></P><PCLASS="para">The reason the multiline Delete command gets the job done is that whenwe encounter two blank lines, the Delete command removes only thefirst of the two.  The next time through the script, the blank linewill cause another line to be read into the pattern space. If thatline is not blank, then both lines are output, thus ensuring that asingle blank line will be output.  In other words, when there are twoblank lines in the pattern space, only the first is deleted. Whena blank line is followed by text, the pattern space is outputnormally.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">DD</SPAN> <SPANCLASS="bibliomisc">from O'Reilly &amp; Associates' <CITECLASS="citetitle">sed &amp; awk</CITE>, Chapter 6</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_17.htm"TITLE="34.17 Searching for Patterns Split Across Lines "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.17 Searching for Patterns Split Across Lines "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_19.htm"TITLE="34.19 Making Edits Everywhere Except... "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.19 Making Edits Everywhere Except... "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">34.17 Searching for Patterns Split Across Lines </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.19 Making Edits Everywhere Except... </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 + -