Chapter 14
Home Up Errata / Updates Why Shell? Using Telnet Telnet Logging Chapters 1&2 Chapters 3&7&13 Chapters 4&5 Chapters 6&8 Chapter 22 Chapter 23 Chapters 10&12 Chapter 14 Chapter 15
Updated:
2000-11-18 15:20

Chapter 14 Exercises

Read Chapter 14 of the Unix text and work on the Unix system. Make sure you can answer the following questions:

Chapter 14     Regular Expressions (grep, egrep, sed, vi, etc.)

 (pages 618-645)

14-1.     What are the advantages of using grep instead of using an editor for a simple searching job?

14-2.     Why is the first argument to grep almost always enclosed in single quotes?

14-3.     What is fgrep?  What is egrep?

14-4.     In the following imaginary grep command line:    grep abc def ghi jkl

abc is interpreted by the utility grep as a(n) _____________
def is interpreted by the utility grep as a(n) _____________
ghi is interpreted by the utility grep as a(n) _____________
jkl is interpreted by the utility grep as a(n) _____________
    [Page 621]

14-5     Explain what the difference is between the following command lines (try them!):

    grep -2 arg
    grep '\-2' arg
    grep \-2 arg

(a) What is the exact text of each argument passed to grep, in each case?
(b) How does grep interpret each argument, in each case?
(c) When is the backslash seen by grep and when is it seen by the shell?

14-6.     In the command line:   grep '[Tt]he' ~/*

(a) How does the shell interpret each of the tokens on the command line?
(b) How many arguments are generated by the shell, to be passed to grep?
(c) How does grep interpret each of those arguments?

14-7.     Explain how the regular expression metacharacters in each of the following patterns work when seen by grep.  What do the patterns match?

	   ^.....
	   xx*$
	   [Tt]he 
	   [bmBM]other 
	   [0-9]A 
	   [09]A 
	   [0-9][0-9] 
	   [A-Z][0-9][A-Z] *[0-9][A-Z][0-9]    (matches postal codes!)
	   [^a-z]
           [\^a-z]
	   [\^]
	   [^^]	
	   ^[^^]      the first  ^ means __________________
		      the second ^ means __________________
		      the third  ^ means __________________
           @[Aa]lgonqu*i*n[Cc]oll*ege *\. *c[ao]m*
	

14-8a.    What pattern instructs grep to locate lines with a $ (dollar sign) at the beginning of the line?  

14-8b.    What pattern instructs grep to locate lines with a ^ (circumflex) at the end of a line?

14-8c.    What pattern instructs grep to locate lines containing a real period (".")?

14-9.     Do the Review 1 questions on page 634 of the text.

Note: The metacharacters \< and \> (page 635) only work inside the vi editor on ACADAIX.

14-10a.     What command line uses grep and the period (".") metacharacter to find all empty lines in a file?  (An empty line is a line that has no characters on it.)

14-10b.     What command line uses grep to find all blank or empty lines in a file?  (In other words, find all lines containing only zero or more space characters.)

14-11.     What command line uses a single egrep command to search for the text FREE or the text $00.00 in the file named costs?  (Test this one!  Make sure it doesn't find $00x00!)  [p.643]

14-12.     Using the input file given on page 619, explain what will be displayed by:
                       grep 's.*s' regexp

14-13.     Using the input file given on page 642, explain what would be displayed by:
                       egrep 'xYY+' test-extend

14-14.     Do the Review 2 questions on page 644 of the text.

Updated November 2, 1999 by Maitang Mark             

Web Author: Ian! D. Allen idallen@ncf.ca
Updated: 2000-11-18 15:20