96 lines
4.9 KiB
HTML
96 lines
4.9 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
|
|
<!Converted with LaTeX2HTML 95.1 (Fri Jan 20 1995) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
|
|
<HEAD>
|
|
<TITLE>3.8.3 Using pipes</TITLE>
|
|
</HEAD>
|
|
<BODY>
|
|
<meta name="description" value="3.8.3 Using pipes">
|
|
<meta name="keywords" value="gs">
|
|
<meta name="resource-type" value="document">
|
|
<meta name="distribution" value="global">
|
|
<P>
|
|
<BR> <HR><A NAME=tex2html3302 HREF="node123.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A> <A NAME=tex2html3300 HREF="node119.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A> <A NAME=tex2html3294 HREF="node121.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <A NAME=tex2html3304 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif"></A> <A NAME=tex2html3305 HREF="node250.html"><IMG ALIGN=BOTTOM ALT="index" SRC="index_motif.gif"></A> <BR>
|
|
<B> Next:</B> <A NAME=tex2html3303 HREF="node123.html">3.8.4 Non-destructive redirection</A>
|
|
<B>Up:</B> <A NAME=tex2html3301 HREF="node119.html">3.8 UNIX Plumbing</A>
|
|
<B> Previous:</B> <A NAME=tex2html3295 HREF="node121.html">3.8.2 Redirecting input and </A>
|
|
<BR> <HR> <P>
|
|
<H2><A NAME=SECTION00583000000000000000>3.8.3 Using pipes</A></H2>
|
|
<P>
|
|
<A NAME=3110> </A>
|
|
We've already demonstrated how to use <tt>sort</tt> as a filter. However,
|
|
these examples assumed that you had data in a file somewhere, or were
|
|
willing to type the data to standard input yourself. What if the
|
|
data you wanted to sort came from the output of another command, such
|
|
as <tt>ls</tt>? For example, using the <tt>-r</tt> option with <tt>sort</tt>
|
|
sorts the data in reverse-alphabetical order. If you wanted to list
|
|
the files in your current directory in reverse order, one way to
|
|
do it would be:
|
|
<P><TT> /home/larry/papers# <em>ls</em> <BR>
|
|
english-list <BR>
|
|
history-final <BR>
|
|
masters-thesis <BR>
|
|
notes <BR>
|
|
/home/larry/papers# <em>ls <b>></b> file-list</em> <BR>
|
|
/home/larry/papers# <em>sort -r file-list</em> <BR>
|
|
notes <BR>
|
|
masters-thesis <BR>
|
|
history-final <BR>
|
|
english-list <BR>
|
|
/home/larry/papers#
|
|
<P></TT>
|
|
Here, we saved the output of <tt>ls</tt> in a file, and then ran
|
|
<tt>sort -r</tt> on that file. But this is unwieldy and causes us
|
|
to use a temporary file to save the data from <tt>ls</tt>.
|
|
<P>
|
|
<A NAME=3123> </A>
|
|
<A NAME=3124> </A>
|
|
The solution is to use <b>pipelining</b>. Pipelining is another
|
|
feature of the shell which allows you to connect a string of
|
|
commands in a ``pipe'', where the stdout of the first
|
|
command is sent directly to the stdin of the second command, and so on.
|
|
Here, we wish to send the stdout of <tt>ls</tt> to the stdin of <tt>sort</tt>.
|
|
The ``<tt>|</tt>'' symbol is used to create a pipe:
|
|
<P><TT> /home/larry/papers# <em>ls <IMG BORDER=0 ALIGN=MIDDLE ALT="" SRC="img185.gif"> sort -r</em> <BR>
|
|
notes <BR>
|
|
masters-thesis <BR>
|
|
history-final <BR>
|
|
english-list <BR>
|
|
/home/larry/papers#
|
|
<P></TT>
|
|
This command is much shorter, and obviously easier to type.
|
|
<P>
|
|
Another useful example---using the command
|
|
<P><TT> /home/larry/papers# <em>ls /usr/bin</em>
|
|
<P></TT>
|
|
is going to display a long list a files, most of which will fly
|
|
past the screen too quickly for you to read them. Instead, let's use
|
|
<tt>more</tt> to display the list of files in <tt>/usr/bin</tt>.
|
|
<P><TT> /home/larry/papers# <em>ls /usr/bin <IMG BORDER=0 ALIGN=MIDDLE ALT="" SRC="img186.gif"> more</em>
|
|
<P></TT>
|
|
Now you can page down the list of files at your own leisure.
|
|
<P>
|
|
But the fun doesn't stop here! We can pipe more than two commands together.
|
|
The command <tt>head</tt> is a filter which displays the first lines from
|
|
an input stream (here, input from a pipe). If we wanted to display
|
|
the last filename in alphabetical order in the current directory, we
|
|
can use:
|
|
<P><TT> /home/larry/papers# <em>ls <IMG BORDER=0 ALIGN=MIDDLE ALT="" SRC="img187.gif"> sort -r <IMG BORDER=0 ALIGN=MIDDLE ALT="" SRC="img188.gif"> head -1</em> <BR>
|
|
notes <BR>
|
|
/home/larry/papers#
|
|
<P></TT>
|
|
where <tt>head -1</tt> simply displays the first line of input that it
|
|
receives (in this case, the stream of reverse-sorted data from <tt>ls</tt>).
|
|
<A NAME=3146> </A>
|
|
<P>
|
|
<BR> <HR><A NAME=tex2html3302 HREF="node123.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A> <A NAME=tex2html3300 HREF="node119.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A> <A NAME=tex2html3294 HREF="node121.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <A NAME=tex2html3304 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif"></A> <A NAME=tex2html3305 HREF="node250.html"><IMG ALIGN=BOTTOM ALT="index" SRC="index_motif.gif"></A> <BR>
|
|
<B> Next:</B> <A NAME=tex2html3303 HREF="node123.html">3.8.4 Non-destructive redirection</A>
|
|
<B>Up:</B> <A NAME=tex2html3301 HREF="node119.html">3.8 UNIX Plumbing</A>
|
|
<B> Previous:</B> <A NAME=tex2html3295 HREF="node121.html">3.8.2 Redirecting input and </A>
|
|
<BR> <HR> <P>
|
|
<BR> <HR>
|
|
<P><ADDRESS>
|
|
<I>Matt Welsh <BR>
|
|
mdw@sunsite.unc.edu</I>
|
|
</ADDRESS>
|
|
</BODY>
|