CSCE 215 May 2017

Lab 3 Solution

The purpose of Lab 3 is for you to get practice using simple pipes and filters.

We will be using text files from your “WorksOfPoe” folder

1. Count the number of users currently on your machine using the commands who and wc, pipelined together.

who | wc -l

2. Next we will display the last three files in the directory/sbin which contains basic programs used for system maintenance or administrative tasks.

ls /sbin | grep mk | sort -r | head -3

3. Use cp to copy the 4 text files from “WorksOfPoe” to your Lab folder. Remember, cp uses a format of:

cp originalfile destinationfile

For me to complete this for Text4.txt from my home directory this looks like:

cp lanebjCSCE215Assignments/WorksOfPoe/Text4.txt lanebjCSCE215Labs/Text4.txt

This will change for you depending on where you currently are in the file directory.

4. Perform a word count on the 1st 3 text files (Text1.txt, Text2.txt, Text3.txt) using cat and wc connected by a pipe.

cat file1 file2 file3 | wc

5. Text4.txt should contain the poem “The Raven.” Confirm this using vi or emacs. If so, exit without changing the file.

6. Use a pipe to create a new file in your Lab folder called “theRavenLab.txt by echoing your name to the file and appending the date.

For me this command looks like:

echo Jeremy Lane > theRavenLab.txt | date > theRavenLab.txt

Again, confirm this using vi or emacs.

7. The Raven maintains a repetition of the "B"rhymethroughout the entire poem -- e.g., "Lenore," "door," "lore." We'll be searching for most of these on “ore” using grep. Use grep to search for “ore”. Do not append this to the theRavenLab.txt file.

8. Once you have the previous step working, use wc like above to count how many matches you found. You will need to use | to pipe the output of grep into wc. Do not append this to the theRavenLab.txt file.

9. If this is working we want to add it to our file under a single line that tells us what the number represents. So, echo and append to your file “Times found ore rhyme” and use pipes to append the number of times you found the rhyme from the previous step to the file. Remember, you may not need to retype everything, use the up and down directional arrows to cycle through your previous commands.

For me this step looks like:

echo "Times found ore rhyme" > theRavenLab.txt | grep "ore" Text4.txt | wc -l > theRavenLab.txt

Check your file to see if this worked. If not, remove the appended text and reattempt the commands from your terminal.

10. Our last step is to add the first 5 lines from the sorted rhyme match to our file. You will need to pipe grep, sort, and head together and append the results to your file.

For me this looks like:

grep "ore" -i Text4.txt | sort | head -5 > theRavenLab.txt

Confirm that this went correctly your file and call me over when you are done. Your file should look similar to:

Jeremy Lane

Wed May 10 11:34:11 EDT 2017

Times found ore rhyme

52

And the only word there spoken was the whispered word, “Lenore?”

By that Heaven that bends above us—by that God we both adore—

By the grave and stern decorum of the countenance it wore,

Clasp a rare and radiant maiden whom the angels name Lenore.”

Darkness there and nothing more.