3. [12 marks] Follow lab 2 instructions to learn how to use UNIX commands sort,
ID: 3880582 • Letter: 3
Question
3. [12 marks] Follow lab 2 instructions to learn how to use UNIX commands sort, uniq and cut before working on this question. This question asks you to use UNIX commands to process a text file which contains information maintained by a publisher about book authors. The pathname of this file on bluenose is /users/faculty/prof2132/public/authors.txt In this file, each line corresponds to an author who has worked with the publisher. Each line has four fields, and they are, from the first field to the last, first name, last name, the number of books authored and the total number of copies printed (of all the books that this author has written). A single space character is used to separate two fields. The first character of each first or last name is capitalized, and the rest are lowercase characters.Explanation / Answer
NOTE: I have completed your assignment. Please check and let me know if you find any issues. I will revert back within 24 hours. thanks for your patience.
I dont see the authors.txt file provided in the question. So i have created sample input file based on understanding from the question. You can run the same commands on the original authors.txt file and verify results. Let me know in case of any queries.
Sample input file used:
Unix Terminal> cat authors.txt
Ravi Krishna 5 100
Hari Rajesh 6 500
Robert Daniel 8 200
Drew Maggie 6 150
Herbert Schildt 5 5000
Marie Andrew 6 19000
Luca King 7 10000
Jonas Leon 3 10000
Robert Lafore 6 20000
Andrew Tannenbaum 9 100000
Unix Terminal>
a)
Command:-
Unix Terminal> sort -k2 -k1 authors.txt > a2q3_a.txt
Unix Terminal> cat a2q3_a.txt
Marie Andrew 6 19000
Robert Daniel 8 200
Luca King 7 10000
Ravi Krishna 5 100
Robert Lafore 6 20000
Jonas Leon 3 10000
Drew Maggie 6 150
Hari Rajesh 6 500
Herbert Schildt 5 5000
Andrew Tannenbaum 9 100000
Unix Terminal>
Explanation:-
1) There is no need to specify the delimiter for sort command because the single space is used in text file
2) sort -k2 -k1 -> It sorts by second key which is last column first then sorts by first column which is key 1
3) Finally the sorted result is stored in the file a2q3_a.txt
b)
Command:-
Unix Terminal> sort -k3 -nr authors.txt > a2q3_b.txt
Unix Terminal> cat a2q3_b.txt
Andrew Tannenbaum 9 100000
Robert Daniel 8 200
Luca King 7 10000
Robert Lafore 6 20000
Marie Andrew 6 19000
Hari Rajesh 6 500
Drew Maggie 6 150
Ravi Krishna 5 100
Herbert Schildt 5 5000
Jonas Leon 3 10000
Unix Terminal>
Explanation:-
1) sort -k3 -> we are sorting the given text file using third column which is number of books authored.
2) sort -k3 -nr -> After sorting we are doing numerical sorting(-n) and then doing reverse sort using -r option.
3) Finally the result is stored in the file a2q3_b.txt
c)
Command:-
Unix Terminal> sort -k4 -nr authors.txt |head -1| cut -d' ' -f1,2 > a2q3_c.txt
Unix Terminal> cat a2q3_c.txt
Andrew Tannenbaum
Explanaiton:-
1) sort -k4 -nr authors.txt
will sort the authors.txt file on 4th column which is number of copies printed and does a reverse numerical sorting. This means the highest number of copies line will come first in the output
2) head -1
After the sorting is performed we are picking the first line which contains author with highest number of copies printed
3) cut -d' ' -f1,2
From the result we are picking only first and second columns which are first and last names of author
4) Finally the result is stored in file a2q3_c.txt
d)
Command:-
Unix Terminal> cat authors.txt | cut -d' ' -f3|sort|uniq|wc -l > a2q3_d.txt
Unix Terminal> cat a2q3_d.txt
6
Explanation:-
1) cut -d' ' -f3
will pick the third column from given file which is number of books authored
2) sort
the number of books authored will be sorted
3) uniq
the uniq command will identify the unique values among the sorted result
4) wc -l
will count number of unique values in sorted result which is distinct number of books authored
5) Finally the result is stored in the file a2q3_d.txt
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.