Starting from:

$30

Assignment 6 SOlution

Introduction: The commands of cut, sort, and uniq are three small but very powerful utility tools in UNIX.




Note:

In CS 3300 UNIX Programming, you should learn them plus quite a few more in depth.
Mac users, bad news! Since the uniq command is not working in exactly the same way as it is in UNIX/Linux, for this assignment (and only this assignment), in order to make my life easier when grading your work, you are asked to use the gargamel server, Google Cloud, or any UNIX/Linux box.


Given the input file emp.lst:




2233!charles harris !g.m. !sales !12/12/52! 90000

9876!bill johnson !director !production!03/12/50!130000

5678!robert dylan !d.g.m. !marketing !04/19/43! 85000

2365!john woodcock !director !personnel !07/11/47!120000

5423!barry wood !chairman !admin !08/30/56!160000

1006!gordon lightfoot!director !sales !09/03/38!140000

6213!michael lennon !g.m. !accounts !06/05/62!105000

1265!p.j. woodhouse !manager !sales !09/12/63! 90000

4290!neil o'bryan !executive!production!09/07/50! 65000

2476!jackie wodehouse!manager !sales !05/01/59!110000

6521!derryk o'brien !director !marketing !09/26/45!125000

3212!bill wilcocks !d.g.m. !accounts !12/12/55! 85000

3564!ronie trueman !executive!personnel !07/06/47! 75000

2345!james wilcox !g.m. !marketing !03/12/45!110000

0110!julie truman !g.m. !marketing !12/31/40! 95000




The following command sequence cuts out the third column (employee title) of the input file, sorts the results alphabetically, and prints only the unique employee titles on the screen. Notice pipes are used to chain up cut, sort, and uniq in the command sequence.




cut –d! –f3 emp.lst | sort | uniq



chairman

d.g.m.

director

executive

g.m.

manager




Another example:




cut –d! –f6 emp.lst | sort –n | uniq -c



1 65000

1 75000

2 85000

2 90000

1 95000

1 105000

2 110000

1 120000

1 125000

1 130000

1 140000

1 160000




STOP HERE: Before you proceed to read the rest of this instruction file, “man” these three commands to find out what options they take and how such options are used.

 

In this assignment, you are asked to implement these three UNIX commands, but in two different ways. See details below.




(10 points) Open the skeleton source file mycut.c and notice that it implements the cut command by calling the very one in the system directly. (Standing on the shoulders of giants, as what they say.) It works but not flexible because all the arguments for cut are hardwired in a string. Here is what you need to do – gather all the arguments (options included) for cut from the command line, combine them into a string, and replace the hardwired “cut ….” with it.



Hint: you should find the built-in functions such as sprintf and strcat very useful.



Your program should run like following and produce exactly the same outputs as cut does:




mycut –d! –f3 emp.lst



Change the options around and see if your program still works.






















(2 points) Now, do the same thing for sort as you did for cut above. (Standing on the shoulders of giants, again!). Test your program mysort by running it like following



mycut –d! –f6 emp.lst | mysort –n



or




mycut –d! –f6 emp.lst salary.lst
mysort –n salary.lst



Change the options around and see if your program still works.







(23 points) Now, the challenging part. Open the skeleton source file myuniq.c and complete it to implement the uniq command on your own, i.e. you are not allowed to call the system version of uniq in your program in any shape, way, or form any more. Read the comments and see if they help you a bit.



Your program only needs to be able to handle any combinations of the following three options for uniq:




-c prefix lines by the number of occurrences

-d only print duplicate lines

-u only print unique lines




You may assume the input data are sorted. Test your program mysort by running it like following:




mycut –d! –f4 emp.lst | mysort | myuniq –c -d



or




mycut –d! –f4 emp.lst | mysort sorteddepts.lst
myuniq –c -d sorteddepts.lst



Change the options around and see if your program still works. Notice that if both –d and –u show as arguments, your program should output nothing except an error message saying “invalid option: -d -u can not co-exist for uniq”.

























What to turn in?

Create a tarball file by the name of cs3335_a6_yourlastname.tar that includes




The completed source code file mycut.c for question 1.
The completed source code file mysort.c for question 2.
The completed source code file myuniq.c for question 3.



Submit the tarball file through BlazeVIEW by the due time.

More products