📄 ch30_22.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 30] 30.22 Filtering Text Through a UNIX Command </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:38Z"><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_21.htm"TITLE="30.21 Per File Setups in Separate Files "><LINKREL="next"HREF="ch30_23.htm"TITLE="30.23 Safer vi Filter-Throughs "></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_21.htm"TITLE="30.21 Per File Setups in Separate Files "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 30.21 Per File Setups in Separate Files "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_23.htm"TITLE="30.23 Safer vi Filter-Throughs "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 30.23 Safer vi Filter-Throughs "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0139">30.22 Filtering Text Through a UNIX Command </A></H2><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="xref.warning"HREF="ch30_23.htm"TITLE="30.23 Safer vi Filter-Throughs ">Warning!</A> </TH><TDVALIGN="TOP">When you're editing in <EMCLASS="emphasis">vi</EM>, you can send a block of text asstandard input to a UNIX command. <ACLASS="indexterm"NAME="AUTOID-33727"></A><ACLASS="indexterm"NAME="AUTOID-33730"></A>The output from this command replaces the block of text in the buffer.You can filter text through a command from either <EMCLASS="emphasis">ex</EM> or <EMCLASS="emphasis">vi</EM>. The main difference between the two methods is that you indicate the block of text with line addresses in <EMCLASS="emphasis">ex</EM> andwith text objects (movement commands) in <EMCLASS="emphasis">vi</EM>.</TD></TR></TABLE><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-139-SECT-1.1">30.22.1 Filtering Text with ex </A></H3><PCLASS="para">The first example demonstrates how to filter text with <EMCLASS="emphasis">ex</EM>.Assume that you're editing a file with a list of names and youwant to alphabetize (sort) lines 96 through 99.You simply type the addresses of the lines you want to filter, followedby an exclamation markand the UNIX command to be executed.For example, the command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>:96,99!sort</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">will pass lines 96 through 99 through the<SPANCLASS="link"><EMCLASS="emphasis">sort</EM> (<ACLASS="linkend"HREF="ch36_01.htm#UPT-ART-7640"TITLE="Putting Things in Order ">36.1</A>)</SPAN>filter and replacethose lines with the output of <EMCLASS="emphasis">sort</EM>.</P><PCLASS="para">The <EMCLASS="emphasis">ex</EM> <CODECLASS="literal">%</CODE> operator is the easiest way to filter all the lines inyour buffer.If you're editing a C program, for instance, you could feed it allthrough theformatting program named <EMCLASS="emphasis">indent</EM>by typing:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">:%!indent</PRE></BLOCKQUOTE></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-139-SECT-1.2">30.22.2 Filtering Text with vi </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-139-IX-VI-EDITOR-FILTERING-TEXT"></A><ACLASS="indexterm"NAME="UPT-ART-139-IX-FILTERS-VI-EDITOR"></A>In <EMCLASS="emphasis">vi</EM>, text is filtered through a UNIX command by typing an exclamation mark (<CODECLASS="literal">!</CODE>)followed by any of <EMCLASS="emphasis">vi</EM>'s movement keystrokesthat indicate a block of text,and then by the UNIX command line to be executed.For example:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>!)</B></CODE><CODECLASS="replaceable"><I>command</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">will pass the next sentence through <EMCLASS="emphasis">command</EM>.</P><PCLASS="para">There are a couple of unusual features about how <EMCLASS="emphasis">vi</EM> acts whenyou use this structure.</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">First, the exclamation mark doesn't appear on your screen right away. Whenyou type the keystroke(s) for the text object you want to filter, theexclamation mark appears at the bottom of the screen, <EMCLASS="emphasis">but thecharacter you type to reference the object does not.</EM></P></LI><LICLASS="listitem"><PCLASS="para">Second, text blocks must be more than one line, so you can use onlythe keystrokes that would move more than one line ( <CODECLASS="literal">G</CODE>, <CODECLASS="literal">{}</CODE>, <CODECLASS="literal">( )</CODE>, <CODECLASS="literal">[[ ]]</CODE>, <CODECLASS="literal">+</CODE>, <CODECLASS="literal">-</CODE> ). To repeatthe effect, a number may precede either the exclamation mark or thetext object. (For example, both <CODECLASS="literal">!10+</CODE> and <CODECLASS="literal">10!+</CODE> wouldindicate the next ten lines.) Objects such as <CODECLASS="literal">w</CODE> do not workunless enough of them are specified so as to exceed a single line.You can also use a slash (<CODECLASS="literal">/</CODE>) followed by a <EMCLASS="emphasis">pattern</EM> and acarriage return to specify the object. This takes the text up to thepattern as input to the command.</P></LI><LICLASS="listitem"><PCLASS="para">Third, there is a special text object that can be used only with this commandsyntax; you can specify the current line by entering a secondexclamation mark:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>!!</B></CODE><CODECLASS="replaceable"><I>command</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"> Remember that either the entire sequence or the text object can be preceded by a number to repeat the effect.For instance, to change lines 96 through 99 as in the previous example,you could position the cursor on line 96 and enter either:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>4!!sort</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">or:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>!4!sort</B></CODE></PRE></BLOCKQUOTE></P></LI></UL><PCLASS="para">As another example, assume you have a portion of text in a message thatyou're going to send to the<SPANCLASS="link">Usenet (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>"net news."<ACLASS="indexterm"NAME="AUTOID-33807"></A>On Usenet, text that might be offensive or would give away the answer toa question is "rotated."The letter <EMCLASS="emphasis">a</EM> is changed to <EMCLASS="emphasis">n</EM>, <EMCLASS="emphasis">b</EM> is changed to <EMCLASS="emphasis">o</EM>,and so on.There are programs for rotating text,but it's also easy to rotate text with the<ACLASS="indexterm"NAME="AUTOID-33813"></A><SPANCLASS="link"><EMCLASS="emphasis">tr</EM> (<ACLASS="linkend"HREF="ch35_11.htm"TITLE="Hacking on Characters with tr ">35.11</A>)</SPAN>command.In this example, the second sentence is the block of text that will be filtered to the command.</P><TABLECLASS="informaltable"><THEADCLASS="thead"><TRCLASS="row"VALIGN="TOP"><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1"></TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"> One sentence before. <UCLASS="cursor">W</U>ith a screen editor you can scroll the page move the cursor, delete lines, insert characters, and more, while seeing the results of your edits as you make them. One sentence after.</PRE></BLOCKQUOTE></TH></TR><TRCLASS="row"VALIGN="TOP"><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Keystrokes</TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Results</TH></TR></THEAD><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><CODECLASS="literal">!)</CODE></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"> One sentence after. ~ ~ ~ !_</PRE></BLOCKQUOTE></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">An exclamation mark appears on the last line to prompt youfor the UNIX command.</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><CODECLASS="literal">tr '[a-z]' '[A-Z]'</CODE></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"> One sentence before. <UCLASS="cursor">W</U>ITH A SCREEN EDITOR YOU CAN SCROLL THE PAGE MOVE THE CURSOR, DELETE LINES, INSERT CHARACTERS, AND MORE, WHILE SEEING THE RESULTS OF YOUR EDITS AS YOU MAKE THEM. One sentence after.</PRE></BLOCKQUOTE></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Enter the UNIX command and press RETURN. The input is replaced by the output.</P></TD></TR></TBODY></TABLE><PCLASS="para">To repeat the previous command, the syntax is:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">! <EMCLASS="emphasis">object</EM> !</PRE></BLOCKQUOTE></P><PCLASS="para">It is sometimes useful to send sections of a coded document to<EMCLASS="emphasis">nroff</EM> to be replaced by formatted output. Remember that the "original" input is replaced by the output. Fortunately, if there is a mistake, such as an error message being sent instead of the expected output, you can undo the command and restore the lines.</P></DIV><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">TOR</SPAN> <SPANCLASS="bibliomisc">from O'Reilly & Associates' <CITECLASS="citetitle">Learning the vi Editor</CITE>, Chapter 7</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_21.htm"TITLE="30.21 Per File Setups in Separate Files "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 30.21 Per File Setups in Separate Files "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_23.htm"TITLE="30.23 Safer vi Filter-Throughs "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 30.23 Safer vi Filter-Throughs "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">30.21 Per File Setups in Separate Files </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.23 Safer vi Filter-Throughs </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 + -