Computer Science 202
Systems Programming Concepts Using C
Lab 4 - Grep Command
Winter 1998


Write a C program using the GNU C Compiler (gcc) that will emulate the functionality of the grep command.


The grep command searches a file for a pattern. Your version is to search a file for all occurrences of a single word. Your program will read in FROM THE COMMAND LINE a single word and the name of a file. It will then search the file for all occurrences of the specified word. If the word is found then it will print out, to the screen, the entire line of text that contains that word. The search is to be case sensitive.

An example session might be something like the following:

    >a.out word data.dat

    Contents of file data.dat:

      There are many words in a file
      and each word has special meaning.
      Words are used everyday
      and they can change our lives.


      There are many words in a file
      and each word has special meaning.

Extra Credit

For extra credit be able to handle the optional argument -i that will allow for a case-insensitive search.


Before the end of class on 4th Tuesday you must demonstrate to your instructor that your program correctly reads in the command line arguments and properly handles incorrect input. Examples of incorrect input are not supplying the correct number of command line arguments or giving an incorrect file name.

The following must be delivered to your instructor via text-only email no later than midnight of 5th Monday February 2, 1998.

  1. Source code for your program.
  2. An example data file that you used to test your program. Note that this file must be different from the one shown above.
In addition, you must deliver to the instructor at the start of lab on 5th Tuesday, February 3, 1998 the following:
  1. A printed copy of your source code.