MCQs on Advanced Regular Expressions | Shell

In advanced shell scripting, regular expressions (regex) provide powerful tools for pattern matching and text manipulation. This section covers advanced pattern matching, using regex with tools like grep, sed, and awk, and complex use cases.


Advanced Pattern Matching

  1. Which of the following symbols represents a “zero or more” match in a regular expression?
    a) *
    b) +
    c) ?
    d) {n,}
  2. What does the regular expression ^abc match?
    a) Lines ending with “abc”
    b) Lines containing “abc” anywhere
    c) Lines starting with “abc”
    d) Lines with exactly “abc”
  3. How would you match any digit in a regular expression?
    a) \d
    b) [0-9]
    c) \w
    d) .
  4. Which of the following is the correct way to escape a special character like a period . in a regular expression?
    a) \\.
    b) \.
    c) /\./
    d) \&.
  5. In regex, what does the [^abc] pattern match?
    a) Any character except a, b, or c
    b) Exactly the characters a, b, or c
    c) Any sequence of a, b, or c
    d) Any sequence of characters that includes a, b, or c

Regex with grep, sed, and awk

  1. Which command is used to search for a regular expression pattern in a file using grep?
    a) grep -r
    b) grep -e
    c) grep -i
    d) grep -p
  2. What does the -P option in grep signify?
    a) Prints the line number
    b) Uses Perl-compatible regex
    c) Ignores case sensitivity
    d) Shows only matching words
  3. How would you delete all lines in a file that contain the word “error” using sed?
    a) sed '/error/d'
    b) sed 'd/error'
    c) sed 'error/d'
    d) sed '/error/clear'
  4. What does the -E flag in grep do?
    a) Enables extended regular expressions
    b) Ignores case sensitivity
    c) Prints only the matched portion
    d) Matches exactly the whole line
  5. Which awk command prints lines where the second column contains the word “apple”?
    a) awk '{print $2}' apple
    b) awk '$2 == "apple" {print}'
    c) awk '/apple/{print $2}'
    d) awk 'print $2 == "apple"'
  6. How can you match any number of spaces between words in a regular expression?
    a) \s+
    b) \d+
    c) \w*
    d) \s*
  7. What does the -i option in grep do?
    a) Inverts the match
    b) Ignores case sensitivity
    c) Outputs line numbers
    d) Matches the entire line
  8. In awk, how would you print the first and third columns of a file?
    a) awk '{print $1, $3}'
    b) awk '$1, $3 {print}'
    c) awk '{print $1:$3}'
    d) awk '$1, $3'
  9. Which command is used with sed to substitute text in a file?
    a) sed 's/find/replace/g'
    b) sed 'replace/find'
    c) sed 'change/find/to/replace'
    d) sed 'find replace'
  10. What does the regular expression \bword\b match?
    a) word at the beginning of a line
    b) word at the end of a line
    c) The exact word “word” as a whole word
    d) Any word containing “word”

Complex Use Cases

  1. How can you match a string of exactly 5 characters using regex?
    a) ^.{5}$
    b) .{5}
    c) ^.....$
    d) .{5,}
  2. Which command can be used to match any line that does not contain the word “error” using grep?
    a) grep -v 'error'
    b) grep '!error'
    c) grep -n 'error'
    d) grep --exclude 'error'
  3. How do you match one or more occurrences of a pattern in a regular expression?
    a) {1,}
    b) +
    c) *
    d) ?
  4. What does the command grep -o '^[0-9]' do?
    a) Matches lines starting with a number
    b) Prints the first digit of each line
    c) Prints the entire line with the first digit removed
    d) Counts the number of digits in each line
  5. What is the purpose of the -f option in sed?
    a) Allows you to specify a file with regex patterns
    b) Forces the substitution to be case-insensitive
    c) Replaces all occurrences of the pattern
    d) Finds and prints all occurrences
  6. What does the regular expression .* match?
    a) Any single character
    b) Zero or more of any character
    c) One or more digits
    d) Any alphabetic character
  7. Which of the following regex patterns matches an email address?
    a) ^[\w]+@[\w]+\.[a-z]{2,}$
    b) ^[a-z0-9]+@[a-z]+\.[a-z]{2,4}$
    c) ^\w+\.\w+@\w+\.com$
    d) [\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$
  8. How do you match a line that starts with “Error” but ends with “failed”?
    a) ^Error.*failed$
    b) Error.*failed^
    c) Error$.*failed
    d) ^Error.*failed
  9. Which of the following regular expressions will match a file path in the form /home/user/filename?
    a) ^/[a-zA-Z0-9/]+$
    b) /home/.*
    c) ^/home/[a-z]+/[a-z]+$
    d) ^/[a-z]+/[a-z]+/[a-z]+$
  10. How do you perform a case-insensitive search using grep?
    a) grep -i
    b) grep -v
    c) grep -c
    d) grep -s
  11. Which of the following regex patterns matches a valid URL?
    a) ^https?://[a-z]+\.[a-z]{2,3}$
    b) ^(http|https):\/\/[a-z0-9]+\.[a-z]{2,4}$
    c) ^https?://[\w]+\.[\w]{2,}$
    d) ^https?://[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
  12. Which of the following patterns will match a string containing at least one uppercase letter?
    a) [A-Z]
    b) [a-z]
    c) \w+
    d) \d+
  13. How do you match a word that starts with a capital letter in regex?
    a) ^[A-Z]\w*$
    b) ^[A-Z]\S*$
    c) ^[A-Z]+$
    d) ^[A-Za-z]+$
  14. What is the output of the following command in Bash: echo "file1.txt file2.txt" | grep -o '\w*\.txt'?
    a) file1.txt file2.txt
    b) file1.txt
    c) file2.txt
    d) file1 file2
  15. What does the command awk '{print $1}' do?
    a) Prints the first column of a file
    b) Prints the first line of a file
    c) Prints the first word of each line
    d) Prints the first character of the first word

Answer Key

QNoAnswer (Option with text)
1a) *
2c) Lines starting with “abc”
3b) [0-9]
4a) \\.
5a) Any character except a, b, or c
6b) grep -e
7b) Uses Perl-compatible regex
8a) sed '/error/d'
9a) Enables extended regular expressions
10b) awk '$2 == "apple" {print}'
11a) \s+
12b) Ignores case sensitivity
13a) awk '{print $1, $3}'
14a) sed 's/find/replace/g'
15c) The exact word “word” as a whole word
16a) ^.{5}$
17a) grep -v 'error'
18b) +
19a) Matches lines starting with a number
20a) Allows you to specify a file with regex patterns
21b) Zero or more of any character
22d) [\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$
23a) ^Error.*failed$
24a) ^/[a-zA-Z0-9/]+$
25a) grep -i
26d) ^https?://[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
27a) [A-Z]
28a) ^[A-Z]\w*$
29b) file1.txt
30a) Prints the first column of a file

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top