Intro to Unix

Assignment: Pipes and Filters

The purpose of this assignment is to demonstrate your ability to use Unix pipes and filters.

The Assignment

For security and administrative reasons, you must use the Unix account supplied to you by the University to prepare and email your assignment --failure to do so will result in your assignment not being graded.

Given a data file of a specified format, create a series of Unix pipes and filters to accomplish various tasks.

Write your Unix pipes and filters to accomplish these tasks in a general way so that it would work on any data file of the same format but different contents.

This assignment description contains a sample data files for you to test your commands. You can assume your data file is called inventory.dat and it is located in the present working directory of the Unix commands.

Important: you must write your assignment so that it works on any data file of this format, but which may contain different contents.

The data file format

The data file presents a simple inventory system of crates and boxes. Here are the rules for that system:

  1. Crates can contain boxes.
  2. Crates cannot contain crates.
  3. Boxes cannot contain crates.
  4. Boxes can contain stuff.
  5. Crates cannot contain stuff that is not in a box.
  6. Stuff can be any noun (and thus will not contain any characters except the letters A through Z in upper or lower case); but any stuff will not contain the string "box" or "crate".
  7. The definition of a box or crate will be written on one line with the crate or box name to the left of an equal sign (=) and the contents written to the right. The contents will be stuff separated by the + sign. The box or crate name will start with the string "box" or "crate", respectively, and be followed immediately (no space) by any number of the digits 0 through 9. (For example, Box682, box007, crate2, CRATE007).
  8. The definition of a box or crate will be written no more than once in the file. (This means that a box or a crate will not be defined two different ways; nor will a box or a crate definition be repeated).
  9. Lines that are not a definition will NOT contain a = sign; these lines are comments.
  10. The names of boxes, crates, or stuff might contain a mix of upper and lower-case letters.
    1. For the box and crate names, the case does not matter. That is "Box1" is the same as "box1" or "BOX1." "Crate007" is the same as "cRAtE1" or "cratE007."
    2. For the stuff, the case does matter. That is, "worms" is different from "Worms" or "WORMS."
    3. For the stuff, all the letters in the name matter. So if we have:
      Box77 = worms
      Box78 = fish + wormshirts
      Box79 = gloworms + rice
      
      Box77 has "worms". Box78 and Box79 do not have "worms" because they have "wormshirts" and "gloworms" respectively.

Here is an example data file of this format; the actual data file I will use to grade your assignment may have different data contents but it will be of the same format; the data file will be called inventory.dat.

* Stuff for the Indies Trip
Note that we are going to have to get Crates from ShopKo.
CRATE2 = Box3
CRatE1 = BOX1 + Box2
Box1 = fish +worms + sand
* Fish YEECH! I hate Box1. Let's not take it.
   Box2 = corn + flour +fishats + wormhats
We are going to have to bring Box2 to the cathedral at Worms.
  CRATE3 = Box4 + Box3 + Box7 + box6 + box33+box99
BOx3 = hay
 BoX7 = gloworms
    box6 = werms + worms+ cats + FISH
-- For goodness' sake, Johnny's got to get some more fish in Box 4
   BoX4 = fish + WORMS + wOrMs
bOx33 =worms + FISH
box99=worms                        

Example task and solution

Display the names of all the boxes that are defined.

$ grep = inventory.dat | grep -vi crate | awk -F= '{print $1}'
Box1
   Box2
BOx3
 BoX7
    box6
   BoX4
bOx33
box99        
$

would get full credit. Note that I had to pay attention to filtering out the crates before using awk to snatch out the box names.

This solution would not get credit:

$ cat inventory.dat | grep -i box
CRATE2 = Box3
CRatE1 = BOX1 + Box2
Box1 = fish +worms + sand
* Fish YEECH! I hate Box1. Let's not take it.
   Box2 = corn + flour +fishats + wormhats
We are going to have to bring Box2 to the cathedral at Worms.
  CRATE3 = Box4 + Box3 + Box7 + box6 + box33+box99
BOx3 = hay
 BoX7 = gloworms
    box6 = werms + worms+ cats + FISH
-- For goodness' sake, Johnny's got to get some more fish in Box 4
   BoX4 = fish + WORMS + wOrMs
bOx33 =worms + FISH
box99=worms                                 
$

The seven tasks you must do

Write Unix pipes and filters to accomplish these tasks on a file in your working directory called inventory.dat which is of the format described above. Read the tasks very carefully and literally. Note that box or crates names and box or crate definitions are different.

Hints

Tasks

The output shown here is based on the example inventory.dat file shown above. Your Unix commands must work for any inventory.dat that conforms to the rules given above with different content.
  1. Display the name(s) of the box(es) that contain worms. (Remember the case matters--"worms" is different from "Worms" or "WOrMs." Also, "worms" is different from "wormsand" or "gloworms". )

    Your output should be:

    Box1
        box6
    bOx33
    box99             
    
  2. Display inventory.dat with all the occurences of the string "fish" changed to "hats". (Remember the case matters--"fish" needs to change to "hats", but "Fish" should not go to hats, nor should "fiSh" change to "hats." BUT: you need not worry what comes before or after the string "fish." That is, if the file has the string "fisherman," you SHOULD change this to "hatserman" -- change the string "fish" to "hats" everywhere it occurs. )

    Your output should be:

    * Stuff for the Indies Trip
    Note that we are going to have to get Crates from ShopKo.
    CRATE2 = Box3
    CRatE1 = BOX1 + Box2
    Box1 = hats +worms + sand
    * Fish YEECH! I hate Box1. Let's not take it.
       Box2 = corn + flour +hatsats + wormhats
    We are going to have to bring Box2 to the cathedral at Worms.
      CRATE3 = Box4 + Box3 + Box7 + box6 + box33+box99
    BOx3 = hay
     BoX7 = gloworms
        box6 = werms + worms+ cats + FISH
    -- For goodness' sake, Johnny's got to get some more hats in Box 4
       BoX4 = hats + WORMS + wOrMs
    bOx33 =worms + FISH
    box99=worms                                
    
  3. Display the names of all crates in alphabetical order, ignoring leading blanks, and folding upper case and lower case letters together.

    Your output should be:

    CRatE1
    CRATE2
      CRATE3       
    
  4. Display all lines of the inventory.dat file that do not contain a box or a crate definition.

    Your output should be:

    * Stuff for the Indies Trip
    Note that we are going to have to get Crates from ShopKo.
    * Fish YEECH! I hate Box1. Let's not take it.
    We are going to have to bring Box2 to the cathedral at Worms.
    -- For goodness' sake, Johnny's got to get some more fish in Box 4      
    
  5. Display just the contents of the boxes with fish (do not display the box names). (it is ok to have a + sign in your display, but not an = sign). (Remember the case matters--"fish" is different from "Fish." The string "fisherman" is different from "fish". The string "glowfish" is different from "fish". )

    Your output should be:

     fish +worms + sand
     fish + WORMS + wOrMs          
    
  6. Display the count of the box(es) that contain worms. (Remember the case matters--"worms" is different from "Worms" or "WOrMs." Also, "worms" is different from "wormsand" or "gloworms".)

    Your output should be:

          4 
    
  7. Display the count of the definitions of crate(s).

    Your output should be:

          3
    

Preparing Your Assignment

For your assignment, you can use Web-based mail (UW-Milwaukee students should use panthermail.uwm.edu) to prepare a list of your answers to the Unix pipes and filters tasks. Alternatively, you could prepare a file using the vi editor.

In this list, put the label "TASKn:" to the left of your answer on the same line for the task number n. For example (note that these are not the correct answers to TASK1 or TASK2), write your answers like this:

TASK1: grep = inventory.dat | sort -u | wc -l
TASK2: grep = inventory.dat | grep -i box 
 .
 .
 .
TASK7: grep = inventory.dat | grep -i box 

Remember, check for a receipt from me to make sure that I received your assignment before you consider the assignment done--remember "It's not complete until you get your receipt!"

Turning in Your Assignment

If you are taking this course for credit, please use these procedures for turning in your assignment. Your assignment will be graded according to criteria I set up for this tutorial.

Save this page to any social bookmarking site! Share · search Search · store Market · star Directory
2017-05-11 · John December · Contact · Terms of Use © December Communications, Inc.