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

📄 ch24_10.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 24] 24.10 zloop: Run a Command on Compressed Files </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:41:37Z"><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="ch24_01.htm"TITLE="24. Other Ways to Get Disk Space"><LINKREL="prev"HREF="ch24_09.htm"TITLE="24.9 How Much Disk Space? "><LINKREL="next"HREF="ch24_11.htm"TITLE="24.11 Edit Compressed Files with zvi, zex, and zed "></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="ch24_09.htm"TITLE="24.9 How Much Disk Space? "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 24.9 How Much Disk Space? "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 24<BR>Other Ways to Get Disk Space</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch24_11.htm"TITLE="24.11 Edit Compressed Files with zvi, zex, and zed "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 24.11 Edit Compressed Files with zvi, zex, and zed "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-4680">24.10 zloop: Run a Command on Compressed Files </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-26656"></A><ACLASS="indexterm"NAME="AUTOID-26658"></A><ACLASS="indexterm"NAME="AUTOID-26661"></A>The good thing about<SPANCLASS="link">compressing files (<ACLASS="linkend"HREF="ch24_07.htm"TITLE="Compressing Files to Save Space ">24.7</A>)</SPAN>is that it saves disk space.The bad thing is that if there are lots of compressed files you want to access separately,typing all those <EMCLASS="emphasis">gzcat</EM> (or <EMCLASS="emphasis">zcat</EM>)commands can get tedious and waste time.</P><PCLASS="para">I wrote a script named <EMCLASS="emphasis">zloop</EM> that takes a command you want to runand a list of compressed files.It runs <EMCLASS="emphasis">gzcat</EM> on each file, separately, and pipes each <EMCLASS="emphasis">gzcat</EM>output to the command you gave.Because <EMCLASS="emphasis">gzcat</EM> also understands <EMCLASS="emphasis">compress</EM> format, it canhandle <EMCLASS="emphasis">.Z</EM> files too.The script shows the command line it ran and the output (if any) of thecommand.If the command returned<SPANCLASS="link">nonzero status (<ACLASS="linkend"HREF="ch44_07.htm"TITLE="Exit Status of UNIX Processes ">44.7</A>)</SPAN>,<EMCLASS="emphasis">zloop</EM> prints a warning.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ls</B></CODE>185.gz  187.gz  189.gz  191.gz  193.gz  195.gz  197.gz186.gz  188.gz  190.gz  192.gz  194.gz  196.gz  198.gz% <CODECLASS="userinput"><B>zloop 'egrep &quot;^Subject:.*group&quot;' *.gz</B></CODE>==== zloop: zcat 185.gz | egrep &quot;^Subject:.*group&quot; ====Subject: List of Active Newsgroups==== zloop: zcat 186.gz | egrep &quot;^Subject:.*group&quot; ====Subject: Alternative Newsgroup Hierarchies==== zloop: zcat 187.gz | egrep &quot;^Subject:.*group&quot; ====zloop: note: that command returned 1 (non-zero) status:   '/usr/local/bin/gzcat 187.gz | egrep &quot;^Subject:.*group&quot;'==== zloop: zcat 188.gz | egrep &quot;^Subject:.*group&quot; ====Subject: Checkgroups message (with INET groups)Subject: Checkgroups message (without INET groups)Subject: Monthly checkgroups posting   ...</PRE></BLOCKQUOTE></P><PCLASS="para"><EMCLASS="emphasis">zloop</EM> is sort of verbose for a UNIX command&nbsp;- but you can make itquieter by editing the script.The status messages are sent to standard error.So,if you want to send <EMCLASS="emphasis">zloop</EM> output through a pager like<EMCLASS="emphasis">more</EM>, tell the shell to merge standard output and standard error:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch13_05.htm"TITLE="13.5 Redirection in C Shell: Capture Errors, Too? ">|&amp;</A> <ACLASS="co"HREF="ch45_21.htm"TITLE="45.21 n&gt;&amp;m: Swap Standard Output and Standard Error ">2&gt;&amp;1</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">.\% <CODECLASS="userinput"><B>zloop 'egrep &quot;^Subject:.*group&quot;' *.gz |&amp; more</B></CODE>   <EMCLASS="emphasis">csh</EM>$ <CODECLASS="userinput"><B>zloop 'egrep &quot;^Subject:.*group&quot;' *.gz 2&gt;&amp;1 | more</B></CODE>   <EMCLASS="emphasis">sh</EM></PRE></TD></TR></TABLE></P><PCLASS="para">With a plain pipe (<CODECLASS="literal">|</CODE>), status messages and command output can be<SPANCLASS="link">jumbled (<ACLASS="linkend"HREF="ch13_04.htm"TITLE="Problems Piping to a Pager ">13.4</A>)</SPAN>.</P><PCLASS="para">In case it isn't clear: when you redirect the output of <EMCLASS="emphasis">zloop</EM>,you're redirecting the output of all the commands that <EMCLASS="emphasis">zloop</EM>runs&nbsp;- i.e., typing this command:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch35_11.htm"TITLE="35.11 Hacking on Characters with tr ">tr</A> <ACLASS="co"HREF="ch35_17.htm"TITLE="35.17 Making Text in Columns with pr ">-2</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>zloop 'tr &quot;[A-Z]&quot; &quot;[a-z]&quot; | pr -2' *.gz &gt; toprint</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para">is like typing these commands by hand:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch13_07.htm"TITLE="13.7 The () Subshell Operators ">(</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">( gzcat <CODECLASS="replaceable"><I>file1</I></CODE>.gz | tr &quot;[A-Z]&quot; &quot;[a-z]&quot; | pr -2  gzcat <CODECLASS="replaceable"><I>file2</I></CODE>.gz | tr &quot;[A-Z]&quot; &quot;[a-z]&quot; | pr -2  gzcat <CODECLASS="replaceable"><I>file3</I></CODE>.gz | tr &quot;[A-Z]&quot; &quot;[a-z]&quot; | pr -2) &gt; toprint</PRE></TD></TR></TABLE></P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="zloop">zloop</A><BR></TH><TDVALIGN="TOP">and feeding the standard output of that<SPANCLASS="link">subshell (<ACLASS="linkend"HREF="ch13_07.htm"TITLE="The () Subshell Operators ">13.7</A>)</SPAN>,and all of the commands,to the <EMCLASS="emphasis">toprint</EM> file.You may never do anything that fancy with <EMCLASS="emphasis">zloop</EM>.The script is on the CD-ROM.</TD></TR></TABLE><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</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="ch24_09.htm"TITLE="24.9 How Much Disk Space? "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 24.9 How Much Disk Space? "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="ch24_11.htm"TITLE="24.11 Edit Compressed Files with zvi, zex, and zed "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 24.11 Edit Compressed Files with zvi, zex, and zed "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">24.9 How Much Disk Space? </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">24.11 Edit Compressed Files with zvi, zex, and zed </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 + -