OpenWindows Advanced User's Guide

4.1.1 grep as a Filter

grep is very often used as a "filter" with other commands. It allows you to filter out useless information from the output of commands. To use grep as a filter, you must pipe the output of the command through grep. The symbol for pipe is "|".

The following example displays files ending in ".ps" that were created in the month of May:

$ ls -l *.ps | grep May

The first part of this command line,

ls -l *.ps

produces a list of files:

$ ls -l *.ps
-rw-r--r--  1 elvis       7228 Apr 22 15:07 change.ps
-rw-r--r--  1 elvis       2356 May 22 12:56 clock.ps
-rw-r--r--  1 elvis       1567 Jun 22 12:56 cmdtool.ps
-rw-r--r--  1 elvis      10198 Jun 22 15:07 command.ps
-rw-r--r--  1 elvis       5644 May 22 15:07 buttons.ps
$

The second part,

| grep May

pipes that list through grep, looking for the pattern May:

$ ls -l *.ps | grep May
-rw-r--r--  1 elvis       2356 May 22 12:56 clock.ps
-rw-r--r--  1 elvis       5644 May 22 15:07 buttons.ps
$