Part 3. Grepping

The grep command is used for searching for text files for a “pattern” and printing out each line that matches the pattern. For example, $ grep vampire 'Stoker, Bram - Dracula.txt' prints out each line containing the word vampire (in lower case). Try it out!

Read grep’s man page to figure out how to perform a case-insensitive search and run the command to print out all lines matching vampire, case insensitively. Hint: typing /case (and then hit enter) while viewing a man page will search for case in the manual. While searching, you can press n/N to go to the next/previous instance.

Your task

Using a text editor, create the file task3.txt in the lab1 directory.

Use grep to print out a count of the lines matching vampire case insensitively. (Search the man page again.) Write the command you used and its output in task3.txt

Open the man page for grep one final time and figure out how to get grep to print the line numbers (and the lines themselves) that match Transylvania and then do that. Write the command you used and its output in task3.txt.

Find and use the command to print out a count of every word in both books. Use the apropos command to search manual pages for keywords. The -a (or --and) option to apropos lets you search for commands that involve all of the key words so $ apropos -a apple sauce will match commands whose descriptions contain both the words apple and sauce. So use apropos -a with appropriate keywords to find a command that produces a word count.

Run that command on all .txt files in the books directory using

$ cmd books/*.txt

where cmd is the command you found with apropos.

Info

The * is called a glob and Bash replaces it with all files that match. In this case, books/*.txt will be replaced with paths to files in the books directory that end with .txt. Globs are powerful and make working with multiple files with similar names easy.

Put the command you ran and its output in task3.txt.

Finally, read the man page for the command to count words in files and find the option to only print line counts. Use the command on both files. Put the command and the output in task3.txt.