old-www/LDP/intro-linux/html/sect_05_03.html

406 lines
6.3 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML
><HEAD
><TITLE
>Filters</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Introduction to Linux"
HREF="index.html"><LINK
REL="UP"
TITLE="I/O redirection"
HREF="chap_05.html"><LINK
REL="PREVIOUS"
TITLE="Advanced redirection features"
HREF="sect_05_02.html"><LINK
REL="NEXT"
TITLE="Summary"
HREF="sect_05_04.html"></HEAD
><BODY
CLASS="sect1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Introduction to Linux: </TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="sect_05_02.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 5. I/O redirection</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="sect_05_04.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="sect_05_03"
></A
>5.3. Filters</H1
><P
>When a program performs operations on input and writes the result to the standard output, it is called a filter. One of the most common uses of filters is to restructure output. We'll discuss a couple of the most important filters below.</P
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="sect_05_03_01"
></A
>5.3.1. More about grep</H2
><P
>As we saw in <A
HREF="sect_03_03.html#sect_03_03_03_04"
>Section 3.3.3.4</A
>, <B
CLASS="command"
>grep</B
> scans the output line per line, searching for matching patterns. All lines containing the pattern will be printed to standard output. This behavior can be reversed using the <TT
CLASS="option"
>-v</TT
> option.
</P
><P
>Some examples: suppose we want to know which files in a certain directory have been modified in February:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;<TT
CLASS="prompt"
>jenny:~&#62;</TT
> <B
CLASS="command"
>ls -la | grep Feb</B
>
</PRE
></FONT
></TD
></TR
></TABLE
><P
>The <B
CLASS="command"
>grep</B
> command, like most commands, is case sensitive. Use the <TT
CLASS="option"
>-i</TT
> option to make no difference between upper and lower case. A lot of GNU extensions are available as well, such as <TT
CLASS="option"
>--colour</TT
>, which is helpful to highlight searchterms in long lines, and <TT
CLASS="option"
>--after-context</TT
>, which prints the number of lines after the last matching line. You can issue a recursive <B
CLASS="command"
>grep</B
> that searches all subdirectories of encountered directories using the <TT
CLASS="option"
>-r</TT
> option. As usual, options can be combined.</P
><P
>Regular expressions can be used to further detail the exact character matches you want to select out of all the input lines. The best way to start with regular expressions is indeed to read the <B
CLASS="command"
>grep</B
> documentation. An excellent chapter is included in the <B
CLASS="command"
>grep</B
> <SPAN
CLASS="application"
>Info</SPAN
> page. Since it would lead us too far discussing the ins and outs of regular expressions, it is strongly advised to start here if you want to know more about them.</P
><P
>Play around a bit with <B
CLASS="command"
>grep</B
>, it will be worth the trouble putting some time in this most basic but very powerful filtering command. The exercises at the end of this chapter will help you to get started, see <A
HREF="sect_05_05.html"
>Section 5.5</A
>.</P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="sect_05_03_02"
></A
>5.3.2. Filtering output</H2
><P
>The command <B
CLASS="command"
>sort</B
> arranges lines in alphabetical order by default:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;<TT
CLASS="prompt"
>thomas:~&#62;</TT
> <B
CLASS="command"
>cat people-I-like | sort</B
>
Auntie Emmy
Boyfriend
Dad
Grandma
Mum
My boss
</PRE
></FONT
></TD
></TR
></TABLE
><P
>But there are many more things <B
CLASS="command"
>sort</B
> can do. Looking at the file size, for instance. With this command, directory content is sorted smallest files first, biggest files last:</P
><P
><B
CLASS="command"
>ls <TT
CLASS="option"
>-la</TT
> | sort <TT
CLASS="option"
>-nk</TT
> <TT
CLASS="parameter"
><I
>5</I
></TT
></B
> </P
><DIV
CLASS="note"
><P
></P
><TABLE
CLASS="note"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="../images/note.gif"
HSPACE="5"
ALT="Note"></TD
><TH
ALIGN="LEFT"
VALIGN="CENTER"
><B
>Old sort syntax</B
></TH
></TR
><TR
><TD
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>You might obtain the same result with <B
CLASS="command"
>ls <TT
CLASS="option"
>-la</TT
> | sort <TT
CLASS="option"
>+4n</TT
></B
>, but this is an old form which does not comply with the current standards.</P
></TD
></TR
></TABLE
></DIV
><P
>The <B
CLASS="command"
>sort</B
> command is also used in combination with the <B
CLASS="command"
>uniq</B
> program (or <B
CLASS="command"
>sort <TT
CLASS="option"
>-u</TT
></B
>) to sort output and filter out double entries:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;<TT
CLASS="prompt"
>thomas:~&#62; </TT
><B
CLASS="command"
>cat <TT
CLASS="filename"
>itemlist</TT
></B
>
1
4
2
5
34
567
432
567
34
555
<TT
CLASS="prompt"
>thomas:~&#62; </TT
><B
CLASS="command"
>sort <TT
CLASS="filename"
>itemlist</TT
> | uniq</B
>
1
2
34
4
432
5
555
567
</PRE
></FONT
></TD
></TR
></TABLE
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="sect_05_02.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="sect_05_04.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Advanced redirection features</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="chap_05.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Summary</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>