Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ grep(1) — AIX/RT 2.2.1

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

ed

sed

sh

grep

PURPOSE

     Searches a file for a pattern.

SYNOPSIS
     grep [ options ] pattern [ files ]

     egrep [ options ] [ pattern ] [ files ]

     fgrep [ options ] [ strings ] [ files ]


DESCRIPTION

     Commands of the grep  family search input files (standard
     input  by   default),  for  lines  matching   a  pattern.
     Normally, they  copy each line found  to standard output.
     Three versions of the grep  command permit you to express
     the matching pattern in varying levels of complexity:

     grep   Searches for  patterns, which are  limited regular
            expressions in the style  of the ed command.  grep
            uses a compact nondeterministic algorithm.
     egrep  Searches  for  patterns  which  are  full  regular
            expressions as in ed, except for "\(" and "\)" and
            with the addition of the following rules:
            o   A regular  expression followed by a  plus sign
                (+)  matches one  or more  occurrences of  the
                regular expression.
            o   A  regular expression  followed by  a question
                mark ("?")  matches 0 or 1  occurrences of the
                regular expression.
            o   Two  regular expressions  separated by  a ver-
                tical bar (|) or by a new-line character match
                strings that are matched by either.
            o   A regular expression may be enclosed in paren-
                theses () for grouping.

            The order  of precedence of operators  is [], then
            "* ?  +", then concatenation,  then | and the new-
            line character.

            The egrep  command uses a  deterministic algorithm
            that needs exponential space.
     fgrep  Searches for patterns which are fixed strings.  It
            searches for lines that contain one of the strings
            (lines are separated by new-line characters).

     All versions  of grep display  the name of the  file con-
     taining the  matched line  if you  specify more  than one
     file name.  Characters with  special meaning to the shell
     ("$ * [ |  ^ ( ) \"), must be quoted  when they appear in
     patterns. When pattern is  not simple string, you usually
     must  enclose  the  entire pattern  in  single  quotation
     marks.  In an  expression such as [a-z],  the minus means
     "through" according to the current collating sequence.  A
     collating sequence may define equivalence classes for use
     in character ranges.  See  the "Overview of International
     Character Support"  in Managing the AIX  Operating System
     for more  information on  collating sequences  and equiv-
     alence classes.

     The exit value of these commands is:

     0  A match was found.
     1  No match was found.
     2  A syntax  error was found  or a file  was inaccessible
        (even if matches were found).

     Notes:

     1.  Lines are limited to 512 characters; longer lines are
         broken into multiple lines of 512 or fewer characters
         (grep only).

     2.  Paragraphs (under the -p  flag) are currently limited
         to a length of 5000 characters.

     3.  Running grep on a special file produces unpredictable
         results and is discouraged.

FLAGS

     -b              Precedes each line by the block number on
                     which  it was  found.  Use  this flag  to
                     help find disk block numbers by context.
     -c              Displays only a count of matching lines.
     -e  pattern     Specifies a pattern.  This works the same
                     as a  simple pattern  but is  useful when
                     the  pattern begins  with a  - (does  not
                     work with grep).
     -f  stringfile  Specifies a  file that  contains patterns
                     (egrep) or strings (fgrep).
     -l              Lists just the names of files (once) with
                     matching lines.  Each  file name is sepa-
                     rated by a new-line character.
     -n              Precedes each line with its relative line
                     number in the file.

     -pparsep        Displays the  entire paragraph containing
                     matched lines.   Paragraphs are delimited
                     by  paragraph  separators, parsep,  which
                     are  patterns in  the  same  form as  the
                     search  pattern.   Lines  containing  the
                     paragraph  separators  are used  only  as
                     separators;  they are  never included  in
                     the output.  The  default paragraph sepa-
                     rator is a blank line (grep only).
     -s              Suppresses error  messages about inacces-
                     sible files (grep only).
     -v              Displays  all  lines  except  those  that
                     match the specified pattern.
     -x              Displays  lines  that match  the  pattern
                     exactly  with  no  additional  characters
                     (fgrep only).

EXAMPLES

     1.  To search several files for  a simple string of char-
         acters:

           fgrep  "strcpy"  *.c

         e

         This searches for the string "strcpy" in all files in
         the current directory with names ending in ".c"
     2.  To count the number of lines that match a pattern:

           fgrep  -c  "{"  pgm.c
           fgrep  -c  "}"  pgm.c

         This  displays the  number of  lines in  "pgm.c" that
         contain open and close braces.

         If you do not put more than  one "{" or "}" on a line
         in your  C programs, and  if the braces  are properly
         balanced, then the two  numbers displayed will be the
         same.  If the numbers are  not the same, then you can
         display the  lines that  contain braces in  the order
         that they  occur in  the file  with:  "  egrep  "{|}"
         pgm.c "
     3.  To use a  pattern that contains some  of the pattern-
         matching characters "*", ^, "?",  [, ], \(, \), \"{",
         and \"}":

           grep  "^[a-zA-Z]"  pgm.s

         This displays all lines in  "pgm.s" that begin with a
         letter.

         Note that  because fgrep does not  interpret pattern-
         matching characters:

           fgrep  "^[a-zA-Z]"  pgm.s

         makes fgrep search only for the string "^[a-zA-Z]" in
         "pgm.s".

     4.  To use an extended pattern  that contains some of the
         pattern-matching characters +, "?", |, (, and ):

           egrep  "\(  *([a-zA-Z]*|[0-9]*)  *\)"  my.txt

         This displays  lines that  contain letters  in paren-
         theses or  digits in  parentheses, but  not parenthe-
         sized  letter-digit combinations.   It matches  "(y)"
         and "(  783902)", but not "(alpha19c)".

         Note:  When using egrep,  \( and \) match parentheses
         in the  text, but  ( and  ")" are  special characters
         that group parts of the pattern.  The reverse is true
         for grep.
     5.  To display all lines that do not match a pattern:

           grep  -v  "^#"

         This displays  all lines that  do not begin with  a #
         character.
     6.  To display the names of files that contain a pattern:

           fgrep  -l  "strcpy"  *.c

         This searches the files in the current directory that
         end with ".c"  and displays the names  of those files
         that contain the string "strcpy".

RELATED INFORMATION

     The following commands:  "ed,"  "sed," and "sh."

     The "Overview of International Character Support" in Man-
     aging the AIX Operating System.

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026