More Filters
Cut and Colrm


Cut

Remember that grep is a filter used to select particular lines for a file.

The cut filter is used to select parts of lines. The format for cut is:
     cut options
where the most common options are:
     -c n-m      -- show only characters n through m
     -f x      -- show only field x
     -d "char"      -- instead of delimiting fields with a tab, use character char

Here are some examples of cut. Suppose we want a list of all commands that deal with copying stuff. "man -k copy" will give us such a list. But, the output of that command also includes a description. (Note that I used the head filter to show just the first 10 lines so the example is not too long.)
> man -k copy | head
acl_copy_entry (3)   - copy an ACL entry
acl_copy_ext (3)     - copy an ACL from internal to external representation
acl_copy_int (3)     - copy an ACL from external to internal representation
asn1_copy_node (3)   - API function
AuSoundCreateDataFromBucket (3nas) - copy the data from a bucket into local m...
AuSoundCreateFileFromBucket (3nas) - copy the data from a bucket into a file.
bcopy (3)            - copy byte sequence
bf_copy (1)          - shell script to copy a bogofilter working directory
BIO_ssl_copy_session_id (3ssl) - SSL BIO
BN_copy (3ssl)       - copy BIGNUMs

So, we can use the cut command to show just part of the line. Here are two examples of using cut. The left example shows columns 1 through 23. The right example is better. It shows only the first field, which is all characters up to the space delimiter.
> man -k copy | cut -c 1-23 | head
acl_copy_entry (3)   -
acl_copy_ext (3)     -
acl_copy_int (3)     -
asn1_copy_node (3)   -
AuSoundCreateDataFromBu
AuSoundCreateFileFromBu
bcopy (3)            -
bf_copy (1)          -
BIO_ssl_copy_session_id
BN_copy (3ssl)       -
> man -k copy | cut -f 1 -d " " | head
acl_copy_entry
acl_copy_ext
acl_copy_int
asn1_copy_node
AuSoundCreateDataFromBucket
AuSoundCreateFileFromBucket
bcopy
bf_copy
BIO_ssl_copy_session_id
BN_copy


colrm

Colrm is short for column remove. While cut shows you only what you asked for, colrm shows you everything that you don't remove.

The format for colrm is:
     colrm start stop
where start and stop are the column numbers to start and stop removal.

Again using the man example:
> man -k copy | colrm 1 23 | head
copy an ACL entry
copy an ACL from internal to external representation
copy an ACL from external to internal representation
API function
cket (3nas) - copy the data from a bucket into local m...
cket (3nas) - copy the data from a bucket into a file.
copy byte sequence
shell script to copy a bogofilter working directory
 (3ssl) - SSL BIO
copy BIGNUMs

So, "colrm 1 23" gives us the opposite of "cut -c 1-23".


Assignment

For your next assignment, edit your majors script so that the -names options will only print students' names; not their name, major, option, etc.

Turn in a printout of your script source code by the beginning of the next lab period.

As an example, if the user provides the -names option, your output should be similar to:

******************************
CSCI    2
Dickson, Anthony James
Downs, Christopher Paul
******************************
CIFS    1
Anderson, Ian
******************************
ACCT    23
Alexander, Amy Elizabeth
Ayers, Brittany Nicole
Brown, Lyeshea Semondre S
Calloway, Logan Mackenzie
Childers, Jamie Leigh
Flick, Heather Wrenee
Foster, Rodney Tyrone
Gallman, Antonio James
Jackson, Miesha Dekezia
Killebrew, Douglas Dennis
Massey, Barbara LaTasha
Nguyen, Julie Yen
Patel, Hardik Rajendrakum
Patterson, Natalie Michel
Raley-Ramos, Chelsea
Robinson, Megan Elizabeth
   yadda 
      yadda
         yadda