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

📄 ch01_04.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 1] 1.4 Using Pipes to Create a New Tool </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:30:56Z"><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="ch01_01.htm"TITLE="1. Introduction"><LINKREL="prev"HREF="ch01_03.htm"TITLE="1.3 Programs Are Designed to Work Together "><LINKREL="next"HREF="ch01_05.htm"TITLE="1.5 Anyone Can Program the Shell "></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="ch01_03.htm"TITLE="1.3 Programs Are Designed to Work Together "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 1.3 Programs Are Designed to Work Together "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 1<BR>Introduction</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch01_05.htm"TITLE="1.5 Anyone Can Program the Shell "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 1.5 Anyone Can Program the Shell "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1032">1.4 Using Pipes to Create a New Tool </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-797"></A>It's easy enough to imagine a trivial use of<SPANCLASS="link">pipes (<ACLASS="linkend"HREF="ch01_03.htm"TITLE="Programs Are Designed to Work Together ">1.3</A>)</SPAN>.Forexample, whenever the output of a command is longer than willfit on a single screen, you might want to pipe to a pagerprogram such as<ACLASS="indexterm"NAME="AUTOID-801"></A><SPANCLASS="link"><EMCLASS="emphasis">more</EM> (<ACLASS="linkend"HREF="ch25_03.htm"TITLE="Using more to Page Through Files ">25.3</A>)</SPAN>,which shows the output a screenful at a time,and waits for you to press a key before it shows the nextscreen.If you were a writer like me, and wanted to checkeach &quot;which&quot; that you wrote to find out if any of themshould have been &quot;that,&quot; you might use the search program<ACLASS="indexterm"NAME="AUTOID-806"></A><SPANCLASS="link"><EMCLASS="emphasis">grep</EM> (<ACLASS="linkend"HREF="ch27_01.htm#UPT-ART-7420"TITLE="Different Versions of grep ">27.1</A>)</SPAN>and type:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch26_04.htm"TITLE="26.4 Using Metacharacters in Regular Expressions ">[Ww]</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>grep '[Ww]hich' chapter1 | more</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para">(Article<ACLASS="xref"HREF="ch13_01.htm#UPT-ART-1023"TITLE="Using Standard Input and Output">13.1</A>has more about pipes.)<EMCLASS="emphasis">more</EM> lets you see the output a screenful at a time.</P><PCLASS="para">However, if you want to see how pipes can be really useful, youneed to be a little more ambitious, or maybe just have a morespecialized problem.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-820"></A><ACLASS="indexterm"NAME="AUTOID-823"></A>For example, the<SPANCLASS="link"><EMCLASS="emphasis">troff</EM> (<ACLASS="linkend"HREF="ch43_13.htm"TITLE="The Text Formatters nroff, troff, ditroff, ... ">43.13</A>)</SPAN>formatting package(used in our office for typesetting some of our books)includes an indexing feature that allows the user toenter indexing commands of the following form:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">.XX &quot;topic, subtopic&quot;</PRE></BLOCKQUOTE></P><PCLASS="para">When the document is printed, the formattingpackage collects these entries, adds page numbers,and assembles the index.It is important that allentries be consistent.For example, if at one point the user makes the entry:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">.XX &quot;Indexing, introduction to&quot;</PRE></BLOCKQUOTE></P><PCLASS="para">and at another point:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">.XX &quot;Index, introduction to&quot;</PRE></BLOCKQUOTE></P><PCLASS="para">the program will generate two separate entriesrather than merging them into one entry with twopage references.</P><PCLASS="para">In order to check the consistency of indexentries, one could enter the following command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat </B></CODE><CODECLASS="replaceable"><I>files</I></CODE><CODECLASS="userinput"><B> | grep .XX | sort -u | more</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-844"></A><ACLASS="indexterm"NAME="AUTOID-848"></A><ACLASS="indexterm"NAME="AUTOID-851"></A><ACLASS="indexterm"NAME="AUTOID-854"></A><ACLASS="indexterm"NAME="AUTOID-858"></A>In this command, <EMCLASS="emphasis">files</EM> is a list of the files to be checked.<EMCLASS="emphasis">grep</EM>searches through that text for a specified string or pattern.[1]<SPANCLASS="link"><EMCLASS="emphasis">sort -u</EM> (<ACLASS="linkend"HREF="ch36_06.htm"TITLE="Miscellaneous sort Hints ">36.6</A>)</SPAN>puts the lines selected by <EMCLASS="emphasis">grep</EM> in alphabetical order andremoves duplicate lines.</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[1] The pattern is a<SPANCLASS="link">regular expression (<ACLASS="linkend"HREF="ch26_04.htm"TITLE="Using Metacharacters in Regular Expressions ">26.4</A>)</SPAN>in which a dot (<CODECLASS="literal">.</CODE>) stands for &quot;any character.&quot;To be precise, use the command <CODECLASS="literal">grep&nbsp;'^\.XX'</CODE> instead.</P></BLOCKQUOTE><PCLASS="para">The pipeline is started with the<SPANCLASS="link"><EMCLASS="emphasis">cat</EM> (<ACLASS="linkend"HREF="ch25_02.htm"TITLE="Four Ways to Skin a cat ">25.2</A>)</SPAN>command,which simply types the files' contents so that theinput to the pipeline will be a single, continuousstream of text.(Otherwise <EMCLASS="emphasis">grep</EM> will print the nameof the file in which the string is found, which willkeep the lines from being sorted correctly.In some versions of <EMCLASS="emphasis">grep</EM>, the <EMCLASS="emphasis">-h</EM> option can be used tosuppress filenames.To see if this works on your UNIX system, type<CODECLASS="literal">grep&nbsp;-h&nbsp;.XX&nbsp;</CODE><CODECLASS="replaceable"><I>files</I></CODE>,omitting <EMCLASS="emphasis">cat</EM> and the pipe.)</P><PCLASS="para">This is a very specific&nbsp;- and normally verytedious&nbsp;- job that needs to be done.And because UNIX provides general-purpose tools and an easyway of using them together in a kind of assemblyline, you are provided a relatively simple way to getthe job done.&#13;</P><PCLASS="para">But...</P><PCLASS="para">&quot;Ugh!&quot; you say, &quot;That's just what I hate about UNIX. Allthese long filenames and options I can't remember. Who wantsto type all that stuff!&quot;</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-884"></A>Precisely. That's why UNIX makes it so easy to create customcommands, in the form of<SPANCLASS="link">aliases (<ACLASS="linkend"HREF="ch10_02.htm"TITLE="Aliases for Common Commands ">10.2</A>)</SPAN>,<SPANCLASS="link">shell functions (<ACLASS="linkend"HREF="ch10_09.htm"TITLE="Shell Functions ">10.9</A>)</SPAN>,and<SPANCLASS="link">shell scripts (<ACLASS="linkend"HREF="ch01_05.htm"TITLE="Anyone Can Program the Shell ">1.5</A>)</SPAN>.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">TOR</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="ch01_03.htm"TITLE="1.3 Programs Are Designed to Work Together "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 1.3 Programs Are Designed to Work Together "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="ch01_05.htm"TITLE="1.5 Anyone Can Program the Shell "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 1.5 Anyone Can Program the Shell "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">1.3 Programs Are Designed to Work Together </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">1.5 Anyone Can Program the Shell </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 + -