Wednesday, June 29, 2005

grep command

rep, egrep, fgrep
search a file for a pattern

----------------------------------------------------------------

SYNTAX
grep [ -bchilnsvw ] limited-regular-expression [filename ... ]

egrep [-bchilnsvx] ... [file ...]

fgrep [-bchilnsv] ... [file ...]

----------------------------------------------------------------

DESCRIPTION
The grep family searches text files for a pattern and prints all lines
that contain that pattern. Be careful using the characters $, *, [, ^,
|, (, ), and \ in the pattern_list because they are also meaningful to
the shell. It is safest to enclose the entire pattern_list in single
quotes '... '.

grep - uses a limited set of regular expressions

fgrep - fast grep, searches for a string not a pattern

egrep - expression grep, uses full regular expressions

Options
-b Precede each line by the block number on which it was found. This
can be useful in locating block numbers by context (first block is 0).

-c
Print only a count of the lines that contain the pattern.

-h
Prevents the name of the file containing the matching line from being
appended to that line. Used when searching multiple files.

-i
Ignore upper/lower case distinction during comparisons.

-l Print only the names of files with matching lines, separated by
NEWLINE characters. Does not repeat the names of files when the
pattern is found more than once.

-n Precede each line by its line number in the file (first line is 1).

-s Suppress error messages about nonexistent or unreadable files.

-v Print all lines except those that contain the pattern.

-w Search for the expression as a word as if surrounded by \< and \>.

---------------------------------------------------------------

EXAMPLES

find info on pppd in the process table:
ps -ef | grep pppd

find all companies in CA in the file database:

grep CA database

find the word disk in upper or lower case in all files:

grep -i disk *

0 Comments:

Post a Comment

<< Home