📄 ch11_07.htm
字号:
CLASS="userinput"><B>echo 1 2 3 4 5</B></CODE>1 2 3 4 5% <CODECLASS="userinput"><B>echo !*</B></CODE>echo 1 2 3 4 51 2 3 4 5</PRE></BLOCKQUOTE></P><PCLASS="para">In an alias: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">alias vcat 'cat -v \!* | more'</PRE></BLOCKQUOTE></P><PCLASS="para"> will pipe the output of<SPANCLASS="link"><EMCLASS="emphasis">cat -v</EM> (<ACLASS="linkend"HREF="ch25_07.htm"TITLE="Show Non-Printing Characters with cat -v or od -c ">25.7</A>)</SPAN>command through <EMCLASS="emphasis">more</EM>.The backslash (<CODECLASS="literal">\</CODE>) has to be there to hide the history character,<CODECLASS="literal">!</CODE>,until the alias is used-see article<ACLASS="xref"HREF="ch10_03.htm"TITLE="C Shell Aliases with Command-Line Arguments ">10.3</A>for more information.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-12726"></A><CODECLASS="userinput"><B>!:2*</B></CODE> gives the second through the last arguments: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>echo 1 2 3 4 5</B></CODE>1 2 3 4 5% <CODECLASS="userinput"><B>echo !:2*</B></CODE>echo 2 3 4 52 3 4 5</PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para"><CODECLASS="userinput"><B>!:2-</B></CODE> like <CODECLASS="literal">2*</CODE> but the last argument is dropped: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>echo 1 2 3 4 5</B></CODE>1 2 3 4 5% <CODECLASS="userinput"><B>echo !:2-</B></CODE>echo 2 3 42 3 4</PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para"><CODECLASS="userinput"><B>!?fn?%</B></CODE> gives the first word found that has <CODECLASS="literal">fn</CODE> in it: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>sort fn1 fn2 fn3</B></CODE> ...% <CODECLASS="userinput"><B>echo !?fn?%</B></CODE>echo fn1fn1</PRE></BLOCKQUOTE></P><PCLASS="para">That found the <CODECLASS="literal">fn</CODE> in <CODECLASS="literal">fn1</CODE>.You can get wilder with: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>echo 1 2 3 4 5</B></CODE>1 2 3 4 5% <CODECLASS="userinput"><B>echo !?ec?^</B></CODE>echo 11</PRE></BLOCKQUOTE></P><PCLASS="para">That selected the command that had <CODECLASS="literal">ec</CODE> in it, and the caret (<CODECLASS="literal">^</CODE>)said to give the first argument of that command.You can also do something like: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>echo fn fn1 fn2</B></CODE>fn fn1 fn2% <CODECLASS="userinput"><B>echo !?fn1?^ !$</B></CODE>echo fn fn2fn fn2</PRE></BLOCKQUOTE></P><PCLASS="para">That cryptic command told the shell to look for a command that had<CODECLASS="literal">fn1</CODE> in it (<CODECLASS="literal">!?fn1?</CODE>), and gave the first argument of thatcommand (<CODECLASS="literal">^</CODE>).Then it gave the last argument (<CODECLASS="literal">!$</CODE>).</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-12775"></A><CODECLASS="userinput"><B>^xy^yx</B></CODE> is the<SPANCLASS="link">shorthand substitution (<ACLASS="linkend"HREF="ch11_03.htm"TITLE="My Favorite Is !$ ">11.3</A>, <ACLASS="linkend"HREF="ch11_05.htm"TITLE="My Favorite Is ^^ ">11.5</A>)</SPAN>command.In the case of: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>echo xxyyzzxx</B></CODE>xxyyzzxx% <CODECLASS="userinput"><B>^xx^ab</B></CODE>echo abyyzzxxabyyzzxx</PRE></BLOCKQUOTE></P><PCLASS="para">it replaced the characters <CODECLASS="literal">xx</CODE> with <CODECLASS="literal">ab</CODE>.This makes editing the previous command much easier.</P></LI><LICLASS="listitem"><PCLASS="para"><CODECLASS="userinput"><B>!!:s/xx/ab/</B></CODE>is doing the same thing as the previous example, but it is using thesubstitute command instead of the <CODECLASS="literal">^</CODE>.This works for any previous command, as in: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>more afile bfile</B></CODE> ...% <CODECLASS="userinput"><B>echo xy</B></CODE>xy% <CODECLASS="userinput"><B>!m:s/b/c/</B></CODE>more afile cfile</PRE></BLOCKQUOTE></P><PCLASS="para">You do not have to use the slashes (<CODECLASS="literal">/</CODE>); any character can act as a delimiter. </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>!!:s:xy:yx</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">There we used the colons (<CODECLASS="literal">:</CODE>) [good when the word you're trying to edit contains a slash-<EMCLASS="emphasis">JP</EM> ].If you want to add more to the replacement, use <CODECLASS="literal">&</CODE> to "replay it" and then addon whatever you like: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>echo xy</B></CODE>xy% <CODECLASS="userinput"><B>!!:s/xy/&yx</B></CODE>echo xyyxxyyx</PRE></BLOCKQUOTE></P><PCLASS="para">The <CODECLASS="literal">&</CODE> in the replacement part said to give what the searchpart found, which was the <CODECLASS="literal">xy</CODE> characters.</P><PCLASS="para">The search part, or left side, cannot include<SPANCLASS="link">metacharacters (<ACLASS="linkend"HREF="ch26_03.htm"TITLE="Understanding Expressions ">26.3</A>)</SPAN>.You must type the actual string you are looking for.</P><PCLASS="para">Also, the example above only replaces the first occurrence of <CODECLASS="literal">xy</CODE>.<ACLASS="indexterm"NAME="AUTOID-12816"></A>To replace them all, use <EMCLASS="emphasis">g</EM>: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>echo xy xy xy xy</B></CODE>xy xy xy xy% <CODECLASS="userinput"><B>!!:s/xy/yx/</B></CODE>echo yx xy xy xyyx xy xy xy% <CODECLASS="userinput"><B>!!:gs/xy/yx/</B></CODE>echo yx yx yx yxyx yx yx yx</PRE></BLOCKQUOTE></P><PCLASS="para">The <EMCLASS="emphasis">g</EM> command in this case meant do all the <CODECLASS="literal">xy</CODE>s.And oddly enough, the <EMCLASS="emphasis">g</EM> has to come before the <EMCLASS="emphasis">s</EM> command.</P><PCLASS="para">Or you could have done:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>echo xy xy xy xy</B></CODE>xy xy xy xy% <CODECLASS="userinput"><B>!!:s/xy/yx/</B></CODE>echo yx xy xy xyyx xy xy xy% <CODECLASS="userinput"><B>!!:g&</B></CODE>echo yx yx yx yxyx yx yx yx</PRE></BLOCKQUOTE></P><PCLASS="para"> In that case, we told the shell to globally (<CODECLASS="literal">:g</CODE>) replace everymatched string from the last command with thelast substitution (<CODECLASS="literal">&</CODE>).Without the <EMCLASS="emphasis">g</EM> command, the shells would have replaced just one more <CODECLASS="literal">xy</CODE> with <CODECLASS="literal">yx</CODE>.</P><PCLASS="para">[A "global" substitution works just once per word:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>echo xyzzy</B></CODE>xyzzy% <CODECLASS="userinput"><B>!!:gs/y/p/</B></CODE>echo xpzzyxpzzy</PRE></BLOCKQUOTE></P><PCLASS="para">The substitution above changed only the first <CODECLASS="literal">y</CODE>. <EMCLASS="emphasis">-TC</EM> ]</P></LI></UL><ACLASS="indexterm"NAME="AUTOID-12849"></A><ACLASS="indexterm"NAME="AUTOID-12850"></A><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">DR</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="ch11_06.htm"TITLE="11.6 Using !$ for Safety with Wildcards "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 11.6 Using !$ for Safety with Wildcards "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="ch11_08.htm"TITLE="11.8 Repeating a Cycle of Commands "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 11.8 Repeating a Cycle of Commands "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">11.6 Using !$ for Safety with Wildcards </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">11.8 Repeating a Cycle of Commands </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 + -