Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ grep(1) — AIX PS/2 1.2.1

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

ed, red

sed

sh, Rsh



GREP, EGREP, FGREP(1,C)     AIX Commands Reference      GREP, EGREP, FGREP(1,C)



-------------------------------------------------------------------------------
grep, egrep, fgrep



PURPOSE

Searches a file for a pattern.

SYNTAX













































Processed November 8, 1990  GREP, EGREP, FGREP(1,C)                           1





GREP, EGREP, FGREP(1,C)     AIX Commands Reference      GREP, EGREP, FGREP(1,C)




        +-------------------+   +------------+
grep ---|    1 +----------+ |---|   +----+   |--->
        +- -p -|          |-+   +---| -s |---+
               +- parsep -+       ^ | -v | |
                                  | | -i | |
                                  | | -w | |
                                  | | -q | |
                                  | +----+ |
                                  +--------+

    +----------------+               +------------+
>---|     one of     |--- pattern ---|            |---|
    |     +----+     |               +--- file ---+
    |     | -c |     |                 ^        |
    | +---| -l |---+ |                 +--------+
    +-|   +----+   |-+
      |   +----+   |
      +---| -b |---+
        ^ | -n | |
        | +----+ |
        +--------+

         +--------+   +----------------+
egrep ---| +----+ |---|     one of     |--->
         +-| -v |-+   |     +----+     |
          ^| -s ||    |     | -c |     |
          || -h ||    | +---| -l |---+ |
          |+----+|    +-|   +----+   |-+
          +------+      |   +----+   |
                        +---| -b |---+
                          ^ | -n | |
                          | +----+ |
                          +--------+

    +---- pattern ----+   +------------+
>---+-  -e pattern   -+---|            |---|
    +- -f stringfile -+   +--- file ---+
                            ^        |
                            +--------+

-----------------
1 No space allowed between these arguments.












Processed November 8, 1990  GREP, EGREP, FGREP(1,C)                           2





GREP, EGREP, FGREP(1,C)     AIX Commands Reference      GREP, EGREP, FGREP(1,C)




         +------------+   +----------------+
fgrep ---|   +----+   |---|     one of     |--->
         +---| -v |---+   |     +----+     |
           ^ | -x | |     |     | -c |     |
           | | -i | |     | +---| -l |---+ |
           | | -s | |     +-|   +----+   |-+
           | | -h | |       |   +----+   |
           | +----+ |       +---| -b |---+
           +--------+         ^ | -n | |
                              | +----+ |
                              +--------+

    +---- pattern ----+   +------------+
>---+-  -e pattern   -+---|            |---|
    +- -f stringfile -+   +--- file ---+
                            ^        |
                            +--------+


DESCRIPTION

Commands of the grep family search input files (standard input, by default) for
lines matching a pattern.  Normally, these commands 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.  The versions are:

grep      Searches for patterns, which are limited regular expressions in the
          style of the ed command.  The grep command uses a compact
          non-deterministic algorithm.

egrep     Searches for patterns which are full regular expressions as in the ed
          command, 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 | (vertical bar) or by a
              new-line character match strings that are matched by either
              expression.

            o A regular expression may be enclosed in () (parentheses) for
              grouping.

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





Processed November 8, 1990  GREP, EGREP, FGREP(1,C)                           3





GREP, EGREP, FGREP(1,C)     AIX Commands Reference      GREP, EGREP, FGREP(1,C)



          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 the grep command display the name of the file containing 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
"Introduction to International Character Support" in Managing the AIX Operating
System for more information on collating sequences and equivalence 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. The maximum line length is 512 characters; longer lines are broken into
    multiple lines of 512 or fewer characters (the grep command only).

  2. Paragraphs (under the -p flag) are limited to a maximum length of 5000
    characters.  If you have selected a language (through the LANG environment
    variable) that supports multibyte characters, the character limits can be
    reduced by as much as 50%, depending on the character code set being used.

  3. Running the grep command 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 -
                      (minus).

-f  stringfile        Specifies a file that contains patterns (egrep) or
                      strings (the fgrep command).




Processed November 8, 1990  GREP, EGREP, FGREP(1,C)                           4





GREP, EGREP, FGREP(1,C)     AIX Commands Reference      GREP, EGREP, FGREP(1,C)



-h                    When multiple files are being processed, suppresses file
                      names (fgrep and egrep only).

-i                    Makes no distinction between uppercase and lowercase
                      characters when making comparisons (the grep and fgrep
                      commands only).

-l                    Lists just the names of files (once) with matching lines.
                      Each file name is separated 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.  If used, the parsep pattern must follow the -p
                      option without a space.  If parsep is not used, the
                      default is a blank line.

-q                    Suppresses error messages about inaccessible files (grep
                      only).

-s                    Silent mode.  Nothing is printed except error messages.
                      This is useful for checking status.  Displays all lines
                      except those that match the specified pattern.

-v                    Displays all lines except those that match the specified
                      pattern.  Returns exit code if lines were found that did
                      not match the pattern.

-w                    The pattern is searched for as a word  (as if surrounded
                      by '\<'pattern'\>').  This flag can be used only with the
                      grep command.

-x                    Displays lines that match the pattern exactly with no
                      additional characters (the fgrep command only).

EXAMPLES

  1. To search several files for a simple string of characters:

      fgrep  "strcpy"  *.c

    This command 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:





Processed November 8, 1990  GREP, EGREP, FGREP(1,C)                           5





GREP, EGREP, FGREP(1,C)     AIX Commands Reference      GREP, EGREP, FGREP(1,C)



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

    This command 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, the two numbers displayed are the
    same.  If the numbers are not the same, 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 command displays all lines in "pgm.s" that begin with a letter.

    Because the fgrep command does not interpret pattern-matching characters,
    the following command makes fgrep search only for the string "^[a-zA-Z]" in
    "pgm.s".

      fgrep  "^[a-zA-Z]"  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 command displays lines that contain letters in parentheses or digits
    in parentheses, but not parenthesized letter-digit combinations.  It
    matches "(y)" and "(  783902)", but not "(alpha19c)".

    Note:  When using the egrep command, \( 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  "^#" pgm.c

    This command displays all lines in pgm.c that do not begin with a #
    character.

  6. To display the names of files that contain a pattern:

      fgrep  -l  "strcpy"  *.c

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



Processed November 8, 1990  GREP, EGREP, FGREP(1,C)                           6





GREP, EGREP, FGREP(1,C)     AIX Commands Reference      GREP, EGREP, FGREP(1,C)




  7. To display all lines containing "$" using grep:

      grep  \\$  file.foo

    This command demonstrates how to quote a special character to find the
    literal character, the "$" in this example.

RELATED INFORMATION

See the following commands:  "ed, red,"  "sed," and  "sh, Rsh."

See "Introduction to International Character Support" in Managing the AIX
Operating System.









































Processed November 8, 1990  GREP, EGREP, FGREP(1,C)                           7



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