CSc 250: Lab 14

Today, you’ll be tasked with writing bash commands (or chains of bash commands) that complete various tasks. You should put each of your solutions into file named lab14.txt, with each problem clearly labeled with a comment. If you are on a Mac, you can do this from the terminal application. If you are on windows 10, you should install bash for windows. You can follow the instructions linked to in the slides. If you don’t have either of these operating systems, you may need to work with another classmate.

You should finish at least four of these problems, and submit your solution to D2L.

Problem 1

Place the below text into a file named people.csv:

njolie,Osborne,eu.ligula@egestas.edu,70
Bertha,Hammond,accumsan.interdum@urna.net,76
Clayton,House,pede@dui.org,42
Elmo,Hull,placerat.augue.Sed@pedemalesuadavel.com,61
Benedict,Hall,metus.Aliquam.erat@Craspellentesque.edu,31
Charissa,Glover,malesuada.malesuada@congueelitsed.ca,89
Thor,Gillespie,Cras@euerat.edu,84
Lawrence,Anthony,odio@dis.ca,36
Oleg,Carter,mi@semperetlacinia.net,33
Chaim,Cohen,tincidunt.neque.vitae@Etiam.ca,76
Henry,Wilkinson,semper@utsem.edu,68

You can also download it: people.csv. Write a chain of shell commands, joined with pipes, to extract all of the email addresses that contain .edu. e result of the command should be:

eu.ligula@egestas.edu
metus.Aliquam.erat@Craspellentesque.edu
Cras@euerat.edu
semper@utsem.edu


Problem 2

Write a sequence of shell commands that extracts the first name and age of all people that are in their 30s. Use the people.csv file from problem 1.

Note: The cut command was mentioned in the class slides, and that it can be used like so:

cut -d ',' -f 2 file.csv

That command retrieves the second column from the CSV data in file.csv. You can select multiple columns at-a-time by entering a comma-separated list of columns, like so:

cut -d ',' -f 2,4 file.csv

You might find this useful for this problem. The output should look along the lines of this:

Benedict,31
Lawrence,36
Oleg,33


Problem 3

Write a chain of shell commands that retrieves the age of the third-oldest person in the data set. Use the people.csv file from problem 1. You should not just hard-code it to return the number of the third-oldest person in this particular file. Rather, it should work for any data-set with this format. For people.csv, it should report 76.


Problem 4

For this problem, you will use this file as input:

names.txt

You should write a command that finds the first name that occurs most often in the data set. You’ll want to use a new command for this one: uniq. The uniq command is used to group together duplicate elements that appear on lines that are above or below each-other. You can also use uniq with the -c option to retrieve the count of how many times the string was seen in-a-row in a file. You’ll want to combine uniq, cut, sort and tail for your solution to this one.


Problem 5

The cal command prints out a month calendar of the current month. For example, running it in April 2018 will give you output along the lines of this:

April 2018
Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30

You should write a sequence of commands that prints out Mo We Fr, and does this by extracting those characters from the cal command. You should not directly print out those strings with echo. You can use cut, sort, tail, reverse, etc to extract these components.


Problem 6

Below is a link to two txt files. Download them, and use them in this and the rest of the problems.

kjv.txt

franklin.txt

Write a sequence of shell commands that displays all of the 15-letter words in the file. You should make sure duplicate words are removed. You’ll also want to use the -E, -o, and -w grep options. Not sure what those do? Use the man page!


Problem 7

In this problem, you’ll use the same input file that you used in the last problem. In this one, use grep to find all sequences of strings with have a 7-letter word, followed by an 8-letter word, followed by a 9-letter word. You should seen the following as output when using kjv.txt:

Abraham reproved Abimelech
hundred priestly vestments
planted pleasant vineyards

And this from franklin.txt:

another asserted something

Problem 8

In this problem, you need to figure out what the 5 most commonly used 12-letter words are from an input file. Words are not necessarily each on their own line in the input file (such as with kjv.txt and franklin.txt). However it can help to get each word onto it’s own line. If you pipe the standard output of a command into this command:

tr ' ' '\n'

This will replace spaces with newlines. You can use this for your solution to this problem, if you need to.

The output from running this on franklin.txt should be:

  13 considerable
  18 acquaintance
  19 Pennsylvania
  20 conversation
  24 Philadelphia

And the output from kjv.txt should be:

  36 transgressed
  38 strengthened
  48 abominations
 104 commandments
 197 congregation