MCQs on Advanced Text Processing | Shell Scripting

Mastering advanced text processing tools in shell scripting is essential for efficient automation. This chapter covers sed, awk, regular expressions, and pattern matching using grep to manipulate and search text files.


Chapter: Advanced Text Processing in Shell Scripting – MCQs

1. In-depth sed and awk Usage

  1. What does the sed command do in Linux?
    • a) Search for patterns in files
    • b) Stream edit text in a file
    • c) Display contents of a file
    • d) Create a new file
  2. Which option in sed is used to perform an in-place edit of a file?
    • a) -i
    • b) -e
    • c) -r
    • d) -p
  3. What is the purpose of the awk command in shell scripting?
    • a) Edit text streams
    • b) Perform pattern-based searches
    • c) Process and analyze text files and streams
    • d) Search files based on regular expressions
  4. Which awk command prints the second column of a file?
    • a) awk '{ print $1 }'
    • b) awk '{ print $2 }'
    • c) awk '{ print 2 }'
    • d) awk '{ $2 }'
  5. In awk, what does $0 represent?
    • a) The first field in the record
    • b) The second field in the record
    • c) The entire record/line
    • d) A variable
  6. Which of the following sed commands would delete a line containing the word “error”?
    • a) sed '/error/d'
    • b) sed '/error/c'
    • c) sed '/error/p'
    • d) sed '/error/s'
  7. Which awk statement is used to calculate the sum of values in the second column of a file?
    • a) awk '{ sum += $2 } END { print sum }'
    • b) awk '{ sum $2 } END { print sum }'
    • c) awk '{ $2 += sum } END { print sum }'
    • d) awk '{ sum = $2 } END { print sum }'
  8. What does the -f option do in the awk command?
    • a) Specifies a field separator
    • b) Executes a script from a file
    • c) Prints the first field of input
    • d) Forces case sensitivity
  9. What is the purpose of the g flag in the sed substitution command?
    • a) Substitute only the first occurrence
    • b) Substitute all occurrences in the line
    • c) Perform global search
    • d) Group characters
  10. In awk, which of the following prints the first three columns of a file?
    • a) awk '{ print $1, $2, $3 }'
    • b) awk '{ print $1, $2 }'
    • c) awk '{ print $1 $2 $3 }'
    • d) awk '{ print $3 }'

2. Regular Expressions in Shell

  1. Which of the following metacharacters in regular expressions represents a match for any single character?
    • a) .
    • b) *
    • c) []
    • d) ?
  2. In a regular expression, which symbol is used to match the beginning of a line?
    • a) ^
    • b) $
    • c) |
    • d) \
  3. What does the regular expression a{2,4} match?
    • a) Exactly 2 occurrences of a
    • b) At least 2 occurrences of a
    • c) Between 2 and 4 occurrences of a
    • d) At most 4 occurrences of a
  4. Which of the following regular expressions matches a line that contains a number?
    • a) [0-9]
    • b) \d
    • c) \w
    • d) [^a-zA-Z]
  5. What is the effect of the + symbol in regular expressions?
    • a) Matches 0 or 1 occurrence of the previous character
    • b) Matches 1 or more occurrences of the previous character
    • c) Matches exactly 1 occurrence of the previous character
    • d) Matches 0 or more occurrences of the previous character
  6. Which command is used to enable extended regular expressions in grep?
    • a) grep -x
    • b) grep -E
    • c) grep -r
    • d) grep -e
  7. In a regular expression, which character matches a whitespace character?
    • a) \w
    • b) \s
    • c) \d
    • d) \t
  8. How would you match exactly 3 digits in a row using a regular expression?
    • a) [0-9]{3}
    • b) [0-9]{3,}
    • c) \d{3}
    • d) \d{3,}
  9. What does the expression [^a-z] in a regular expression match?
    • a) Any lowercase letter
    • b) Any character except lowercase letters
    • c) Only digits
    • d) Only special characters
  10. Which of the following represents an optional character in regular expressions?
    • a) *
    • b) +
    • c) ?
    • d) {}

3. Grep and Pattern Matching

  1. What does the grep command do?
    • a) Finds files based on names
    • b) Searches for patterns in files
    • c) Replaces patterns in files
    • d) Deletes files based on patterns
  2. Which option is used with grep to perform a case-insensitive search?
    • a) -i
    • b) -c
    • c) -l
    • d) -r
  3. What command would you use to search for the word “error” in a file named log.txt?
    • a) grep error log.txt
    • b) find error log.txt
    • c) search error log.txt
    • d) grep -w error log.txt
  4. Which grep command option is used to show the count of matched lines?
    • a) -c
    • b) -n
    • c) -v
    • d) -l
  5. Which of the following grep options shows the line number where the match occurs?
    • a) -n
    • b) -c
    • c) -v
    • d) -l
  6. How would you search for the word “data” in multiple files in a directory?
    • a) grep "data" *
    • b) grep -r "data" *
    • c) grep -l "data" *
    • d) grep -f "data" *
  7. What does the -v option in grep do?
    • a) Shows only matching lines
    • b) Shows non-matching lines
    • c) Shows line numbers of matches
    • d) Shows the filenames where matches occur
  8. Which grep command option is used to show only the filenames containing the match?
    • a) -l
    • b) -v
    • c) -c
    • d) -n
  9. What is the purpose of grep -r?
    • a) To search for a pattern recursively in directories
    • b) To print the count of matches
    • c) To show matches in reverse order
    • d) To search for a specific regular expression
  10. Which of the following grep options would invert the match, showing lines that do not contain the pattern?
    • a) -v
    • b) -n
    • c) -i
    • d) -l

Answers

QnoAnswer
1b) Stream edit text in a file
2a) -i
3c) Process and analyze text files and streams
4b) awk '{ print $2 }'
5c) The entire record/line
6a) sed '/error/d'
7a) awk '{ sum += $2 } END { print sum }'
8b) Executes a script from a file
9b) Substitute all occurrences in the line
10a) awk '{ print $1, $2, $3 }'
11a) .
12a) ^
13c) Between 2 and 4 occurrences of a
14a) [0-9]
15b) Matches 1 or more occurrences of the previous character
16b) grep -E
17b) \s
18a) [0-9]{3}
19b) Any character except lowercase letters
20c) ?
21b) Searches for patterns in files
22a) -i
23a) grep error log.txt
24a) -c
25a) -n
26b) grep -r "data" *
27b) Shows non-matching lines
28a) -l
29a) To search for a pattern recursively in directories
30a) -v

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