Lab Assignment--Pipes

(Please do this lab in LINUX)
  1. Try out some pipes and filters on the command line.
    1. Copy the following text into a file called colorfile.txt.
      This is an example file
      testing the use of grep and sed.
      Sed will modify the spelling of color.
      Red color, blue color, green color, orange color.
      Grep will display specific lines.  
      This is for our pipes lab.
      Pipes provide the user with a synchronized means of
      interprocess communication.
      Two kinds of pipes exist: named or unnamed.
    2. Use the grep command on colorfile.txt to display all the lines not containing the word "color" with the line number appearing in the output. Store the result in a file called color.txt. Record the command that you used to do this as a comment at the beginning of your question 2 code.

  2. Using the system calls from this lab, your goal is to create a program that executes the following command which will count the number of occurrences of "color" in the file and write the results to newfile.txt.
    	grep -o color colorfile.txt | wc -w > newfile.txt 

    More details:

    1. Create two argument vectors:
      1. One for grep -o color colorfile.txt
      2. One for wc -w

      An example argument vector would be:

      char *myargv[]={(char *)"ls", (char *)"-l", NULL};
    2. create a pipe
    3. fork the process
    4. child will call dup2 to hook standard output to one end of the pipe. Then, execute the grep command using execvp
    5. parent will call dup2 to hook standard input to the other end of the pipe. Then, execute the wc command using execvp
    6. parent will also call dup2 to hook standard output to the file called newfile.txt

Sample run for question 2:

% pipes
% cat newfile.txt

Deliverables (2 files):