Tuesday, June 17, 2008

grep

Use this command to search for information in a file or files. For example, suppose that we have a file dict whose contents are

   red rojo
green verde
blue azul
white blanco
black negro
Then we can look up items in our file like this;
   % grep red dict
red rojo
% grep blanco dict
white blanco
% grep brown dict
%

Notice that no output was returned by grep brown. This is because "brown" is not in our dictionary file.

Grep can also be combined with other commands. For example, if one had a file of phone numbers named "ph", one entry per line, then the following command would give an alphabetical list of all persons whose name contains the string "Fred".

   % grep Fred ph | sort
Alpha, Fred: 333-6565
Beta, Freddie: 656-0099
Frederickson, Molly: 444-0981
Gamma, Fred-George: 111-7676
Zeta, Frederick: 431-0987
The symbol "|" is called "pipe." It pipes the output of the grep command into the input of the sort command.

For more information on grep, consult

   % man grep 

No comments: