COMP 2718 Lab 7

Student Name -- Student Number

This lab should be completed by editing and submitting this HTML file and by submitting scripts and output files.

Submit via all materials by D2L by the end of the lab session.

Exercise 1: Built-in using while and shift

Write and test a script called, builtin_while.sh that uses a while loop and the shift command to solve Exercise 1 from Lab 6. Your script should be tested with:

    ./builtin_while.sh cd ls read for echo ps bind w then declare shift help printf grep

Submit builtin_while.sh.

Exercise 2: Colour, Time, or Money

A file contains lines, where each line contains either the name of colour, the time, or dollar amount. The colour is either red, green or blue. Time is in the format HH:MM, where HH is the hour and MM is the minutes. The dollar amount varies from $X.YY to $XXX,XXX.YY. After $999.99, a comma is added to separate the thousands.

A sample file is:

red
$1,000.34
12:01
green
a random line
01:33
01:87
$34.15
4.15
blue

There are two valid times, two dollar amounts, and three colours. Copy the lines into a file called ex2.txt. Write a shell script that uses regular expressions and counts the number of lines with HH:MM time, the number lines with dollar amounts, and the number lines with a colour. (hint: use three different regular expressions).

  1. Your script should be called, count_without_grep.sh. As the name suggests, this script should not use grep. ex2.txt should be passed as the first argument to this script.

  2. Write a script to do the same as above only using grep (hint: three grep commands are sufficient). This script should be called count_with_grep.sh.

Exercise 3: Sentence recognizer

A file contains two kind of sentences

The possible toys are: car, truck, ball, or rope. The colours are: red, green, blue, orange, yellow, or brown. The animals are: cat, dog, or human. The verbs are: licks, chases, "picks up", or avoids. There can be one or more spaces between words. There can also be sentences that are different than the above.

The test input file is:

The cat licks  the human.
The rope is green.
A random sentence.
The cat is blue.
The  dog  chases  the   cat.
The  ball is purple.

Write a script called sentence_rec.sh that counts the two kinds of sentences, and outputs the count. The script should also echo the line that matches one of the sentence kinds. The kind of sentence should be identified. The script should be called with the input file as its only argument.

Exercise 4: Grepping Words

This exercise makes reference to the words file from Lab 2. If you don't have this file it can be obtained from the following respository:

$ git clone https://gitlab.com/COMP2718/lab2_repos.git

Also, you should use the specified variant of grep for each question.

  1. Find all the lines in words that contain 5 a's, using regular grep (i.e., basic regular expressions). abracadabra is one of the words.

    Change this with your answer

  2. Find all the lines in words that contain 5 a's, using extended regular expressions. That is, use grep -E. Your answer should shorter than the previous question.

    Change this with your answer

  3. Count the number of lines in words that contain at least 4 e's and at most 5 e's, using extended grep -E.

    Change this with your answer

  4. Count the number of lines in words that contain exactly 3 letters where the middle letter is a vowel (a, e, i, o, u) using grep (i.e. basic regular expressions).

    Change this with your answer

  5. Count the number lines in words that that start with t and contain the sequence ae or ea. using grep -E.

    Change this with your answer

  6. The last exercise can not be solved with basic regular expression (why?). Instead two greps, sort and uniq and wc can be combined in a pipeline to solve the problem. Combine the greps with (grep xy words; grep yx words) so that their standard outputs are combined. What is the pipeline?

    Change this with your answer