📄 ch30_15.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 30] 30.15 Useful Global Commands (with Pattern Matches) </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_14.htm"TITLE="30.14 Moving Blocks of Text by Patterns "><LINKREL="next"HREF="ch30_16.htm"TITLE="30.16 Counting Occurrences; Stopping Search Wraps "></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_14.htm"TITLE="30.14 Moving Blocks of Text by Patterns "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 30.14 Moving Blocks of Text by Patterns "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_16.htm"TITLE="30.16 Counting Occurrences; Stopping Search Wraps "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 30.16 Counting Occurrences; Stopping Search Wraps "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-6350">30.15 Useful Global Commands (with Pattern Matches) </A></H2><PCLASS="para">The best way to learn pattern matching is by example, so here's a short list of pattern-matching examples, with explanations.(Article<ACLASS="xref"HREF="ch26_10.htm"TITLE="Pattern Matching Quick Reference with Examples ">26.10</A>has a list of these patterns.)Study the syntax carefully so you understand the principles at work.You should then be able to adapt these examples to your own situation.</P><OLCLASS="orderedlist"><LICLASS="listitem"><PCLASS="para">Change all occurrences of the word <EMCLASS="emphasis">help</EM>(or <EMCLASS="emphasis">Help</EM>) to <EMCLASS="emphasis">HELP</EM>:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch33_03.htm"TITLE="33.3 Line Addressing ">%</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen"><CODECLASS="userinput"><B>:%s/[Hh]elp/HELP/g</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para">or:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>:%s/[Hh]elp/\U&/g</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">The <CODECLASS="literal">\U</CODE> changes the pattern that follows to all uppercase. Thepattern that follows is the repeated search pattern, which iseither <EMCLASS="emphasis">help</EM> or <EMCLASS="emphasis">Help</EM>. </P></LI><LICLASS="listitem"><PCLASS="para">Replace one or more spaces following a colon (<CODECLASS="literal">:</CODE>) or a period(<CODECLASS="literal">.</CODE>) with two spaces (here a space is marked by a \h'1p'<IMGSRC="../chars/squ.gif"ALT=" ">\h'1p'):</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>:%s/\([:.]\\)\h'1p'<IMGSRC="../chars/squ.gif"ALT=" ">\h'1p'\h'1p'<IMGSRC="../chars/squ.gif"ALT=" ">\h'1p'*/\\1\h'1p'<IMGSRC="../chars/squ.gif"ALT=" ">\h'1p'\h'1p'<IMGSRC="../chars/squ.gif"ALT=" ">\h'1p'/g</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Either of the two characters within brackets can be matched.This character is saved into a<SPANCLASS="link">hold buffer, using <CODECLASS="literal">\(</CODE> and <CODECLASS="literal">\)</CODE> (<ACLASS="linkend"HREF="ch34_10.htm"TITLE="Referencing Portions of a Search String ">34.10</A>)</SPAN>,and restored on the right-hand side by the <CODECLASS="literal">\1</CODE>.Note that within brackets a special character such as a dot does not need to be escaped with a backslash (<CODECLASS="literal">\</CODE>).</P></LI><LICLASS="listitem"><PCLASS="para">Delete all blank lines:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch33_04.htm"TITLE="33.4 Useful ex Commands ">g</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen"><CODECLASS="userinput"><B>:g/^$/d</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para">What you are actually matching here is the beginning of the line (<CODECLASS="literal">^</CODE>)followed by the end of the line (<CODECLASS="literal">$</CODE>), with nothing in between.</P></LI><LICLASS="listitem"><PCLASS="para">Delete all blank lines, plus any lines that contain only white space:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>:g/^[<IMGSRC="../chars/squ.gif"ALT=" "></B></CODE><CODECLASS="replaceable"><I>tab</I></CODE><CODECLASS="userinput"><B>]*$/d</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">(In the line above, a tab is shown as <CODECLASS="replaceable"><I>tab</I></CODE>.)A line may appear to be blank but may in fact contain spaces or tabs.The previous example will not delete such a line.This example, like the one above it, searches for the beginning and endof the line. But instead of having nothing in between, thepattern tries to find any number of spaces or tabs.If no spaces or tabs are matched, the line is blank.To delete lines that contain white space but that <EMCLASS="emphasis">aren't</EM> blank,you would have to match lines with <EMCLASS="emphasis">at least</EM> one space or tab:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>:g/^[<IMGSRC="../chars/squ.gif"ALT=" "></B></CODE><CODECLASS="replaceable"><I>tab</I></CODE><CODECLASS="userinput"><B>][<IMGSRC="../chars/squ.gif"ALT=" "></B></CODE><CODECLASS="replaceable"><I>tab</I></CODE><CODECLASS="userinput"><B>]*$/d</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para">Match the first quoted argument of all section header (<CODECLASS="literal">.Ah</CODE>)<SPANCLASS="link">macros (<ACLASS="linkend"HREF="ch43_13.htm"TITLE="The Text Formatters nroff, troff, ditroff, ... ">43.13</A>)</SPAN>andreplace each line with this argument:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>:%s/^\.Ah "\([^"]*\)" .*/\1/</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">The substitution assumes that the <CODECLASS="literal">.Ah</CODE> macro can have more thanone argument surrounded by quotes. You want to match everythingbetween quotes, but only up to the <EMCLASS="emphasis">first</EM> closing quote. Using<CODECLASS="literal">".*"</CODE> would be wrong because it would match all argumentson the line. What you do is match a series of characters that<EMCLASS="emphasis">aren't</EM> quotes, <CODECLASS="literal">[^"]*</CODE>. The pattern<CODECLASS="literal">"[^"]*"</CODE> matches a quote, followed by any number ofnon-quote characters, followed by a quote. Enclose the first argumentin <CODECLASS="literal">\(</CODE> and <CODECLASS="literal">\)</CODE> so that it can be replayed using<CODECLASS="literal">\1</CODE>.</P></LI><LICLASS="listitem"><PCLASS="para">Same as previous, except preserve the original lines by copying them:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>:g/^\.Ah/t$ | s/\.Ah "\([^"]*\)" .*/\1/</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">In <EMCLASS="emphasis">ex</EM>, the vertical bar (<CODECLASS="literal">|</CODE>) is a command separatorthat works like a<SPANCLASS="link">semicolon (<CODECLASS="literal">;</CODE>) (<ACLASS="linkend"HREF="ch08_05.htm"TITLE="Command-Line Evaluation ">8.5</A>)</SPAN>on a UNIX command line. The first part, <CODECLASS="literal">:g/^\.Ah/t$</CODE>,matches all lines that begin with a <CODECLASS="literal">.Ah</CODE> macro, uses the<CODECLASS="literal">t</CODE> command to copy these lines, and places them after the lastline (<CODECLASS="literal">$</CODE>) of the file. The second part is the same as inthe previous example, except that the substitutions are performed oncopies at the end of the file. The original lines are unchanged.<ACLASS="indexterm"NAME="AUTOID-33441"></A><ACLASS="indexterm"NAME="AUTOID-33442"></A><ACLASS="indexterm"NAME="AUTOID-33443"></A><ACLASS="indexterm"NAME="AUTOID-33444"></A></P></LI></OL><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">TOR</SPAN>, <SPANCLASS="authorinitials">DG</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_14.htm"TITLE="30.14 Moving Blocks of Text by Patterns "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 30.14 Moving Blocks of Text by Patterns "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_16.htm"TITLE="30.16 Counting Occurrences; Stopping Search Wraps "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 30.16 Counting Occurrences; Stopping Search Wraps "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">30.14 Moving Blocks of Text by Patterns </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.16 Counting Occurrences; Stopping Search Wraps </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 + -