📄 ch30_14.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 30] 30.14 Moving Blocks of Text by Patterns </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:45:27Z"><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="ch30_01.htm"TITLE="30. vi Tips and Tricks"><LINKREL="prev"HREF="ch30_13.htm"TITLE="30.13 Appending to an Existing File "><LINKREL="next"HREF="ch30_15.htm"TITLE="30.15 Useful Global Commands (with Pattern Matches) "></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="ch30_13.htm"TITLE="30.13 Appending to an Existing File "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 30.13 Appending to an Existing File "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 30<BR>vi Tips and Tricks</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch30_15.htm"TITLE="30.15 Useful Global Commands (with Pattern Matches) "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 30.15 Useful Global Commands (with Pattern Matches) "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-6340">30.14 Moving Blocks of Text by Patterns </A></H2><PCLASS="para">You can move blocks of text delimited by<SPANCLASS="link">patterns (<ACLASS="linkend"HREF="ch30_09.htm"TITLE="Using Search Patterns and Global Commands">30.9</A>)</SPAN>.<ACLASS="indexterm"NAME="UPT-ART-6340-IX-PATTERNS-EX-EDITOR"></A><ACLASS="indexterm"NAME="UPT-ART-6340-IX-VI-EDITOR-PATTERNS"></A><ACLASS="indexterm"NAME="UPT-ART-6340-IX-EX-EDITOR-PATTERNS"></A><ACLASS="indexterm"NAME="UPT-ART-6340-IX-PATTERNS-VI-EDITOR"></A><ACLASS="indexterm"NAME="AUTOID-33307"></A><ACLASS="indexterm"NAME="AUTOID-33310"></A>For example, assume you have a 150-page reference manual.All references pages are organized into three paragraphs withthe same three headings: SYNTAX, DESCRIPTION, and PARAMETERS.A sample of one reference page follows:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"> .Rh 0 "Get status of named file" "STAT" .Rh "SYNTAX" .nf integer*4 stat, retval integer*4 status(11) character*123 filename ... retval = stat (filename, status) .fi .Rh "DESCRIPTION" Writes the fields of a system data structure into the status array. These fields contain (among other things) information about the file's location, access privileges, owner, and time of last modification. .Rh "PARAMETERS" .IP "\fBfilename\fR" 15n A character string variable or constant containing the UNIX pathname for the file whose status you want to retrieve. You can give the ...</PRE></BLOCKQUOTE></P><PCLASS="para">Suppose that it is decided to move the SYNTAX paragraph below theDESCRIPTION paragraph.Using pattern matching, you can move blocks of text on all 150 pageswith one command!</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>:g/SYNTAX/,/DESCRIPTION/-1 mo /PARAMETERS/-1</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">This command operates on the block oftext between the line containing the word <EMCLASS="emphasis">SYNTAX</EM> and the line justbefore the word <EMCLASS="emphasis">DESCRIPTION</EM> (<CODECLASS="literal">/DESCRIPTION/-1</CODE>).The block is moved (using <CODECLASS="literal">mo</CODE>)to the line just before <EMCLASS="emphasis">PARAMETERS</EM>(<CODECLASS="literal">/PARAMETERS/-1</CODE>).Note that <EMCLASS="emphasis">ex</EM> can place text only below the line specified.To tell <EMCLASS="emphasis">ex</EM> to place text above a line, you first have to move up a line with <CODECLASS="literal">-1</CODE>, and then place your text below.In a case like this, one command saves literally hours of work.(This is a real-life example - we once used a pattern match like thisto rearrange a reference manual containing hundreds of pages.)</P><PCLASS="para">Block definition by patterns can be used equally well with other <EMCLASS="emphasis">ex</EM> commands.For example, if you wanted to delete all DESCRIPTION paragraphs inthe reference chapter, you could enter:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>:g/DESCRIPTION/,/PARAMETERS/-1d</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-33335"></A><ACLASS="indexterm"NAME="AUTOID-33338"></A>This very powerful kind of change is implicit in <EMCLASS="emphasis">ex</EM>'s lineaddressing syntax, but it is not readily apparent even to experiencedusers. For this reason, whenever you are faced with a complex,repetitive editing task, take the time to analyze the problem and findout if you can apply pattern-matching tools to do the job.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">TOR</SPAN> <SPANCLASS="bibliomisc">from O'Reilly & Associates' <CITECLASS="citetitle">Learning the vi Editor</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="ch30_13.htm"TITLE="30.13 Appending to an Existing File "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 30.13 Appending to an Existing File "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="ch30_15.htm"TITLE="30.15 Useful Global Commands (with Pattern Matches) "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 30.15 Useful Global Commands (with Pattern Matches) "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">30.13 Appending to an Existing File </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">30.15 Useful Global Commands (with Pattern Matches) </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 & 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 + -