📄 ch09_24.htm
字号:
></P><PCLASS="para">This works with most any UNIX command - not just <EMCLASS="emphasis">rcp</EM>.For example, if I want to print the files, I can use:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>lpr `cat /tmp/bk$$`</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para">If I'll be doing a lot of work with the filenames, typing the backquotesover and over is a pain.I'll store the filenames in a<SPANCLASS="link">shell variable (<ACLASS="linkend"HREF="ch06_08.htm"TITLE="Shell Variables ">6.8</A>)</SPAN>:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">First, pick a name for the shell variable.I usually choose <EMCLASS="emphasis">temp</EM> because it's not used on my account.You should check the name you choose before you set it by typing <CODECLASS="literal">echo</CODE><CODECLASS="literal">"$</CODE><CODECLASS="replaceable"><I>varname</I></CODE><CODECLASS="literal">"</CODE>; be sure it's empty or useless.</P></LI><LICLASS="listitem"><PCLASS="para">Store the filenames from your temporary file:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>set temp=(`cat /tmp/bk$$`)</B></CODE> <ICLASS="lineannotation">...C shells</I> $ <CODECLASS="userinput"><B>temp="`cat /tmp/bk$$`"</B></CODE> <ICLASS="lineannotation">...Bourne shells</I></PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para">Use the shell variable.For example:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cp $temp</B></CODE><CODECLASS="replaceable"><I> backupdir</I></CODE>% <CODECLASS="userinput"><B>vi $temp</B></CODE>% ...</PRE></BLOCKQUOTE></P></LI></UL></LI></OL><PCLASS="para">(Sometimes,<SPANCLASS="link"><EMCLASS="emphasis">find</EM> with operators like <EMCLASS="emphasis">-newer</EM> (<ACLASS="linkend"HREF="ch17_08.htm"TITLE="Exact File Time Comparisons ">17.8</A>)</SPAN>is easier - but <EMCLASS="emphasis">find</EM> searches subdirectories too,unless you use<SPANCLASS="link"><EMCLASS="emphasis">-prune</EM> (<ACLASS="linkend"HREF="ch17_23.htm"TITLE="Finding Files with -prune ">17.23</A>)</SPAN>.)Oh, try to remember to <EMCLASS="emphasis">rm</EM> the temp file when you're done with it.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-7630-SECT-1.2">9.24.2 Search for Text with grep </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-11385"></A><ACLASS="indexterm"NAME="AUTOID-11388"></A>The <EMCLASS="emphasis">grep</EM><SPANCLASS="link"><EMCLASS="emphasis">-l</EM> option (<ACLASS="linkend"HREF="ch15_07.htm"TITLE="Getting a List of Matching Files with grep -l ">15.7</A>)</SPAN>gives you a list of filenames that match a string - that might be all Ineed.But sometimes I need to see the lines that <EMCLASS="emphasis">grep</EM> found to decidewhether to include that file or not.Here's how:</P><OLCLASS="orderedlist"><LICLASS="listitem"><PCLASS="para">Search the files.Be sure that your <EMCLASS="emphasis">grep</EM>-like command will print the filename beforeeach matching line:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>egrep -i "summar(y|ies)" * > /tmp/bk$$</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para">Edit the temporary file.The lines will look something like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">a0066:Here is a summary of the different components:a0183:Summary: String functions in awka0183:for a summary of all the string functionsa0184:Let's start wlth a short summary how awk treats commanda1000:Here's a summary of the rules that UNIX uses to interpret paths:a1000:Here's a summary of the different sorts of wildcards available:a1680:cumulative summary files and ASCII reports in a2710:In summary, \fIcomm\fP is similar to \fIdiff\fP: ...</PRE></BLOCKQUOTE></P><PCLASS="para">Leave a line for each file that you want to operate on; delete the rest:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">a0066:Here is a summary of the different components:a0183:Summary: String functions in awka1000:Here's a summary of the different sorts of wildcards available: ...</PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para">Strip off everything after the filenames.Unless any of your filenames have colons (<CODECLASS="literal">:</CODE>) in them, youcan tell <EMCLASS="emphasis">vi</EM> to strip off the colons and everything after them.That command is:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>:%s/:.*//</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">If there's a chance that a filename might be repeated (because<EMCLASS="emphasis">grep</EM> matched more than one line), filter the filenames through<EMCLASS="emphasis">sort -u</EM> to get rid of duplicates.In <EMCLASS="emphasis">vi</EM>, type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>:%!sort -u</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Like before, what's left is a list of filenames:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">a0066a0183a1000 ...</PRE></BLOCKQUOTE></P><PCLASS="para"> You can feed them to whatever command you need to run, as in theprevious example.</P></LI></OL><PCLASS="para">I hope those two examples give you the idea, which is: learn what UNIX utilitiesare "out there"-and how to grab and edit their output to do what you want.</P></DIV><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="ch09_23.htm"TITLE='9.23 Workaround for "Arguments too long" Error'><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT='Previous: 9.23 Workaround for "Arguments too long" Error'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="ch09_25.htm"TITLE="9.25 The C Shell repeat Command "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 9.25 The C Shell repeat Command "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">9.23 Workaround for "Arguments too long" Error</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">9.25 The C Shell repeat Command </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 + -