Pipes, grep, and wc


Pipes

As we have seen is previous assignments, you can direct the output of a program directly into a file.  For example, to dump a long directory listing into a file named "bob":
     ls -al > bob

A linux user can also direct the output of a program into another program.  For example, suppose you want to view a long directory listing, but the listing is more than one page in length.  You can view the listing one page at a time by piping the output of the ls program into the input of the more program:
     ls -al | more

You can put many pipes together.  But we will save that for later.


grep

The grep program is a very useful filter.  Given a series of lines of text, grep will filter out all lines except the lines that match a provided string.

The ps command gives you a detailed list all running processes, probably page after page of process information. So, to find a process named bob that has gone nuts, you could use the following command:
    ps -aux | grep bob
That grep command eliminates all lines except lines containing the letters bob.  In short, you see just the one line that you want.

You can also reverse the filter.  You can use grep -v to invert grep and show you all lines that do not contain a certain string. For example, the command
    ls  -l | grep *.cpp
will show you the fiile names of just your source code files.  While the command
    ls -l | grep -v *.cpp
will show you all your files that are not source code files.

And to see the first ten files that are not source code files:
    ls -l | grep -v *.cpp | head


wc

The wc program counts lines, words, and characters. It can do all three counts, or just some of the counts.

For example, suppose the who command yields the following output
    > who
    ACC.dannellys2 pts/0 2010-09-21 10:19 (cbacsci03.win.winthrop.edu)

That output piped through wc provides the following output
    > who | wc
    1     5     74

In other words, wc counted 1 line, 5 words, and 74 characters.

Note that wc can not read English, so it thinks "(cbacsci03.win.winthrop.edu)" is one word, not four words.

Using the options -l for lines, -w for words, and -c for characters, you can limit what wc counts.


Assignment

First, copy the file named "lab9.txt" from dannelly's home directory.  Go to the directory where you want the data file to be copied to, then type:
    cp ~ACC.dannellys2/lab9.txt .

Using the grep and wc commands, write one line commands to
1 - count the number of CSCI majors that graduated
2 - count the number of CIFS option students that graduated
3 - count the number of non-CIFS business majors (this one is tricky)

By the way, the correct counts are 2, 1, and 128.