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
Which of the following symbols represents a “zero or more” match in a regular expression? a) * b) + c) ? d) {n,}
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”
How would you match any digit in a regular expression? a) \d b) [0-9] c) \w d) .
Which of the following is the correct way to escape a special character like a period . in a regular expression? a) \\. b) \. c) /\./ d) \&.
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
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
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
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'
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
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"'
How can you match any number of spaces between words in a regular expression? a) \s+ b) \d+ c) \w* d) \s*
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
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'
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'
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
How can you match a string of exactly 5 characters using regex? a) ^.{5}$ b) .{5} c) ^.....$ d) .{5,}
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'
How do you match one or more occurrences of a pattern in a regular expression? a) {1,} b) + c) * d) ?
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
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
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
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}$
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
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]+$
How do you perform a case-insensitive search using grep? a) grep -i b) grep -v c) grep -c d) grep -s
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,}$
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+
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]+$
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
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
QNo
Answer (Option with text)
1
a) *
2
c) Lines starting with “abc”
3
b) [0-9]
4
a) \\.
5
a) Any character except a, b, or c
6
b) grep -e
7
b) Uses Perl-compatible regex
8
a) sed '/error/d'
9
a) Enables extended regular expressions
10
b) awk '$2 == "apple" {print}'
11
a) \s+
12
b) Ignores case sensitivity
13
a) awk '{print $1, $3}'
14
a) sed 's/find/replace/g'
15
c) The exact word “word” as a whole word
16
a) ^.{5}$
17
a) grep -v 'error'
18
b) +
19
a) Matches lines starting with a number
20
a) Allows you to specify a file with regex patterns