hw07: Counting cats
- Due before the beginning of class on Wednesday, January 24
This homework will ask you to download a directory of files containing “fortunes” from the linux command-line “game” with that name, and then do some text processing on those files using a bash script.
Files download
Follow this link to download fortunes.tgz, or run the following on the command line:
wget "https://roche.work/courses/s24sd212/hw/bash/fortunes.tgz"
This is a compressed archive of an entire folder. After saving the tgz file into your current directory, run this command to extract it:
tar -xzf fortunes.tgz
That should create a new subdirectory called fortunes
which has a
bunch of text files about magic and pets.
Your task
Write a bash script called cats.sh
that looks in the fortunes
folder
for the text files about pets, and for each one, displays the filename
and a count of how many times that file contains the word “cat”.
The first few lines of the output when running the command bash cats.sh
should look exactly like this:
fortunes/pets-01.txt: 0
fortunes/pets-02.txt: 2
fortunes/pets-03.txt: 0
fortunes/pets-04.txt: 1
fortunes/pets-05.txt: 1
fortunes/pets-06.txt: 1
fortunes/pets-07.txt: 7
fortunes/pets-08.txt: 0
fortunes/pets-09.txt: 0
fortunes/pets-10.txt: 1
...
Hints:
- Use a
for
loop - Don’t worry about checking if “cat” is inside some other word or by itself. Just look for “cat” on each line.
- The search for “cat” should be case-insensitive. (Run
grep --help
orman grep
to find the option that tells it to ignore upper or lower case.) - There are some files which are not about pets, but about magic. Don’t look for cats there.
- You need to use
echo
to show the filename, but without adding a new line (so that you can also display the count). Runhelp echo
to find out the option forecho
that prevents it from displaying a newline at the end.
Submit command
To submit files for this homework, run one of these commands:
submit -c=sd212 -p=hw07 cats.sh
club -csd212 -phw07 cats.sh