Saturday, 3 November 2012

grep - Basic Unix Commands


grep

This command is used to search for information in a file or files.
For example, suppose that we have a file colors whose contents are
   red cloth
   green grass
   black sea
   white elephant
   blue whale
   yellow paper

Then we can search  items in our file like this;
   % grep blue colors
      blue whale
   % grep red colors
     red cloth
   
Grep can also be combined with other commands. For example, if one had a file of phone numbers named "mob", 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

No comments:

Post a Comment