Skip to content

Quiz: Text Processing with Grep and Regular Expressions

Test your understanding of pattern matching and text searching.


1. What does grep stand for and what does it do?

  1. General Regular Expression Parser - compiles code
  2. Global Regular Expression Print - searches for patterns in text
  3. Group Regular Expression Processor - sorts files
  4. Graphical Regular Expression Program - displays images
Show Answer

The correct answer is B. grep (Global Regular Expression Print) searches for patterns in text files or input streams and prints matching lines. It's one of the most frequently used commands for finding information in files and logs.

Concept Tested: Grep Command

See: Chapter 8 - Grep Command


2. What does grep -i "error" logfile.txt do?

  1. Inverts the search, showing non-matching lines
  2. Performs a case-insensitive search for "error"
  3. Counts the number of matches
  4. Shows only the first match
Show Answer

The correct answer is B. The -i flag makes grep case-insensitive, so it matches "error", "Error", "ERROR", and any other case variation. This is useful when you don't know the exact capitalization.

Concept Tested: Grep Options

See: Chapter 8 - Grep Options


3. In regular expressions, what does the . (dot) character match?

  1. Only a literal period
  2. Any single character except newline
  3. Zero or more characters
  4. The end of a line
Show Answer

The correct answer is B. In regex, . is a wildcard that matches any single character (except newline). For example, c.t matches "cat", "cot", "cut", etc. To match a literal period, escape it with \..

Concept Tested: Regular Expressions

See: Chapter 8 - Regex Basics


4. What does ^ mean at the start of a regex pattern?

  1. Matches a literal caret character
  2. Matches the beginning of a line
  3. Matches the end of a line
  4. Negates the entire pattern
Show Answer

The correct answer is B. The ^ anchor matches the beginning of a line. So ^Error matches "Error" only when it appears at the start of a line, not in the middle. Use $ to match the end of a line.

Concept Tested: Regular Expressions, Anchors

See: Chapter 8 - Anchors


5. What does grep -r "TODO" . do?

  1. Searches only the current file
  2. Recursively searches all files in the current directory and subdirectories
  3. Reverses the search results
  4. Removes lines containing "TODO"
Show Answer

The correct answer is B. The -r (recursive) flag tells grep to search through all files in the specified directory and its subdirectories. The . means start from the current directory.

Concept Tested: Grep Options, Recursive Search

See: Chapter 8 - Recursive Search


6. What does * mean in a regular expression?

  1. Matches any characters (like a shell wildcard)
  2. Matches zero or more of the preceding character
  3. Matches exactly one character
  4. Matches the start of a file
Show Answer

The correct answer is B. In regex, * means "zero or more of the preceding element." So ab*c matches "ac", "abc", "abbc", "abbbc", etc. This is different from shell wildcards where * matches any characters.

Concept Tested: Regular Expressions, Quantifiers

See: Chapter 8 - Quantifiers


7. What does grep -v "pattern" file.txt do?

  1. Verbose output with extra details
  2. Shows only lines that DON'T match the pattern
  3. Validates the pattern syntax
  4. Shows version information
Show Answer

The correct answer is B. The -v flag inverts the match, showing lines that do NOT contain the pattern. This is useful for filtering out unwanted lines, like grep -v "^#" config.txt to remove comment lines.

Concept Tested: Grep Options

See: Chapter 8 - Inverting Matches


8. What does the regex pattern [0-9]+ match?

  1. Exactly one digit
  2. One or more digits
  3. Zero or more digits
  4. The literal string "[0-9]+"
Show Answer

The correct answer is B. [0-9] is a character class matching any digit, and + means "one or more." So [0-9]+ matches one or more consecutive digits like "42" or "12345". Note: + requires extended regex (grep -E).

Concept Tested: Regular Expressions, Character Classes

See: Chapter 8 - Character Classes


9. What command shows the count of matching lines instead of the lines themselves?

  1. grep -l
  2. grep -c
  3. grep -n
  4. grep -o
Show Answer

The correct answer is B. The -c flag tells grep to output only the count of matching lines, not the lines themselves. For example, grep -c "error" logfile.txt outputs just a number like "42".

Concept Tested: Grep Options

See: Chapter 8 - Counting Matches


10. What's the difference between grep and grep -E (or egrep)?

  1. -E is faster
  2. -E enables extended regular expressions with more features
  3. -E searches in binary files
  4. -E encrypts the output
Show Answer

The correct answer is B. grep -E (or egrep) uses extended regular expressions, which support additional features like + (one or more), ? (zero or one), | (alternation), and grouping without escaping. Basic grep requires escaping these.

Concept Tested: Extended Regular Expressions

See: Chapter 8 - Extended Regex