📄 ch33_04.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 33] 33.4 Useful ex Commands </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:46:48Z"><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="ch33_01.htm"TITLE="33. Batch Editing"><LINKREL="prev"HREF="ch33_03.htm"TITLE="33.3 Line Addressing "><LINKREL="next"HREF="ch33_05.htm"TITLE="33.5 Running Editing Scripts Within vi "></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="ch33_03.htm"TITLE="33.3 Line Addressing "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 33.3 Line Addressing "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 33<BR>Batch Editing</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch33_05.htm"TITLE="33.5 Running Editing Scripts Within vi "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 33.5 Running Editing Scripts Within vi "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7997">33.4 Useful ex Commands </A></H2><PCLASS="para">Many line editor commands are not particularly useful in scripts.<ACLASS="indexterm"NAME="UPT-ART-7997-IX-EX-EDITOR-COMMAND-DESCRIPTIONS"></A><ACLASS="indexterm"NAME="UPT-ART-7997-IX-COMMANDS-EX-EDITOR"></A>The two commands that you will use far and away the most often are<BCLASS="emphasis.bold">s</B> (substitute), to replace one pattern with another, and<BCLASS="emphasis.bold">d</B> (delete) to delete one or more lines. On occasion, though,you'll want to insert text from a script. (<SPANCLASS="link">Editing scripts builtby <EMCLASS="emphasis">diff</EM> (<ACLASS="linkend"HREF="ch28_09.htm"TITLE="ex Scripts Built by diff ">28.9</A>)</SPAN>make heavy use of insert,append, delete, and change commands.) And of course, you needcommands to write and quit the editor.</P><PCLASS="para">Here's the syntax of most of the commands you may encounter in<EMCLASS="emphasis">ex</EM> editing scripts.(The <EMCLASS="emphasis">ed</EM> editor understands the abbreviated versions of some,but not all of, these commands.)Elements in [brackets] are optional; don't type the <CODECLASS="literal">[</CODE> or <CODECLASS="literal">]</CODE>.(The leading colon is the <EMCLASS="emphasis">ex</EM> command characterused to issue an <EMCLASS="emphasis">ex</EM> commandfrom <EMCLASS="emphasis">vi</EM>; in a script, the colon would be omitted.)</P><DLCLASS="variablelist"><DTCLASS="term"><BCLASS="emphasis.bold">append</B></DT><DDCLASS="listitem"><PCLASS="para">[address] <BCLASS="emphasis.bold">a</B>[<BCLASS="emphasis.bold">!</B>]<EMCLASS="emphasis">text</EM><CODECLASS="userinput"><B>\.</B></CODE><ACLASS="indexterm"NAME="AUTOID-36495"></A>Append <EMCLASS="emphasis">text</EM> at specified <EMCLASS="emphasis">address</EM>, or at present address ifnone is specified. Add a <BCLASS="emphasis.bold">!</B> to switch the <BCLASS="emphasis.bold">autoindent</B>setting that will be used during input. For example, if <BCLASS="emphasis.bold">autoindent</B>was enabled, <BCLASS="emphasis.bold">!</B> disables it.</P></DD><DTCLASS="term"><BCLASS="emphasis.bold">change</B></DT><DDCLASS="listitem"><PCLASS="para">[address] <BCLASS="emphasis.bold">c</B>[<BCLASS="emphasis.bold">!</B>]<EMCLASS="emphasis">text</EM><CODECLASS="userinput"><B>\.</B></CODE>Replace the specified lines with <EMCLASS="emphasis">text</EM>. Add a <BCLASS="emphasis.bold">!</B> to switch<ACLASS="indexterm"NAME="AUTOID-36514"></A>the <BCLASS="emphasis.bold">autoindent</B> setting during input of <EMCLASS="emphasis">text</EM>.</P></DD><DTCLASS="term"><BCLASS="emphasis.bold">copy</B></DT><DDCLASS="listitem"><PCLASS="para">[address] <BCLASS="emphasis.bold">co</B> destination[address] <BCLASS="emphasis.bold">t</B> destinationCopy the lines included in <EMCLASS="emphasis">address</EM> to the specified<EMCLASS="emphasis">destination</EM> address.<ACLASS="indexterm"NAME="AUTOID-36527"></A></P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">:1,10 co 50:1,10t50</PRE></BLOCKQUOTE></P><PCLASS="para"></P></DD><DTCLASS="term"><BCLASS="emphasis.bold">delete</B></DT><DDCLASS="listitem"><PCLASS="para">[address] <BCLASS="emphasis.bold">d</B> [buffer]Delete the lines included in <EMCLASS="emphasis">address</EM>.<ACLASS="indexterm"NAME="AUTOID-36539"></A>If <EMCLASS="emphasis">buffer</EM> is specified, save or append the text to the named buffer.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">:/Part I/,/Part II/-1d <EMCLASS="emphasis">Delete to line above "Part II"</EM>:/main/+d <EMCLASS="emphasis">Delete line below "main"</EM>:.,$/d <EMCLASS="emphasis">Delete from this line to last line</EM></PRE></BLOCKQUOTE> </P></DD><DTCLASS="term"><BCLASS="emphasis.bold">global</B></DT><DDCLASS="listitem"><PCLASS="para">[address] <BCLASS="emphasis.bold">g</B>[<BCLASS="emphasis.bold">!</B>]<BCLASS="emphasis.bold">/</B><EMCLASS="emphasis">pattern</EM><BCLASS="emphasis.bold">/</B>[<EMCLASS="emphasis">commands</EM>]Execute <EMCLASS="emphasis">commands</EM> on all lines that contain <EMCLASS="emphasis">pattern</EM> or, if<ACLASS="indexterm"NAME="AUTOID-36560"></A><EMCLASS="emphasis">address</EM> is specified, on all lines within that range.If <EMCLASS="emphasis">commands</EM> are not specified, print all such lines.(Exception: doesn't print when you use it from <EMCLASS="emphasis">vi</EM> by typing<CODECLASS="literal">:</CODE> first. You'll need to add a <EMCLASS="emphasis">p</EM>, as in the second example below).If <BCLASS="emphasis.bold">!</B> is used, execute <EMCLASS="emphasis">commands</EM> on all lines that <EMCLASS="emphasis">don't</EM>contain <EMCLASS="emphasis">pattern</EM>.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">:g/Unix/:g/Unix/p:g/Name:/s/tom/Tom/</PRE></BLOCKQUOTE></P><PCLASS="para"></P></DD><DTCLASS="term"><BCLASS="emphasis.bold">insert</B></DT><DDCLASS="listitem"><PCLASS="para">[address] <BCLASS="emphasis.bold">i</B>[<BCLASS="emphasis.bold">!</B>]<EMCLASS="emphasis">text</EM><CODECLASS="userinput"><B>\.</B></CODE>Insert <EMCLASS="emphasis">text</EM> at line before the specified <EMCLASS="emphasis">address</EM>, or at present<ACLASS="indexterm"NAME="AUTOID-36585"></A>address if none is specified. Add a <BCLASS="emphasis.bold">!</B> to switch the<BCLASS="emphasis.bold">autoindent</B> setting during input of <EMCLASS="emphasis">text</EM>. </P></DD><DTCLASS="term"><BCLASS="emphasis.bold">move</B></DT><DDCLASS="listitem"><PCLASS="para">[address] <BCLASS="emphasis.bold">m</B> destinationMove the lines specified by <EMCLASS="emphasis">address</EM> to the <EMCLASS="emphasis">destination</EM> address.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">:.,/Note/m /END/ <EMCLASS="emphasis">Move block after line containing "END"</EM></PRE></BLOCKQUOTE></P><PCLASS="para"></P></DD><DTCLASS="term"><BCLASS="emphasis.bold">print</B></DT><DDCLASS="listitem"><PCLASS="para">[address] <BCLASS="emphasis.bold">p</B> [count]Print the lines specified by <EMCLASS="emphasis">address</EM>.<ACLASS="indexterm"NAME="AUTOID-36609"></A><EMCLASS="emphasis">count</EM> specifies the number of lines to print, starting with <EMCLASS="emphasis">address</EM>.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">:100;+5p <EMCLASS="emphasis">Show line 100 and the next five lines</EM></PRE></BLOCKQUOTE></P><PCLASS="para"></P></DD><DTCLASS="term"><BCLASS="emphasis.bold">quit</B></DT><DDCLASS="listitem"><PCLASS="para"><BCLASS="emphasis.bold">q</B>[<BCLASS="emphasis.bold"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -