Regular Expressions and Text Processing in Bash Quiz Quiz

Assess your understanding of using regular expressions and basic text processing commands in Bash. This quiz covers core concepts such as pattern matching, search and replace, and common utilities like grep, sed, and awk for efficiently handling and manipulating text in shell scripts.

  1. Basic Regex Matching

    Which Bash command with regular expressions would you use to search for lines containing the word 'world' in the file 'greetings.txt'?

    1. awk 'world' greetings.txt
    2. grep 'world' greetings.txt
    3. find 'world' greetings.txt
    4. sed 'world' greetings.txt

    Explanation: The 'grep' command is specifically used for searching text patterns within files, making it ideal for this task. While 'find' is used to locate files, not content within files. The 'sed' and 'awk' commands are more suited for advanced text processing and require additional syntax to achieve a simple search, making them less appropriate here.

  2. Anchors in Regular Expressions

    In a Bash script, what does the regular expression '^abc' match in a line of text?

    1. Lines containing 'abc' anywhere
    2. Lines ending with 'abc'
    3. Lines starting with 'abc'
    4. Only the exact string 'abc'

    Explanation: The caret (^) at the beginning of a regular expression indicates the start of a line, so '^abc' matches lines that begin with 'abc'. The dollar sign ($) would specify the end of a line, not the caret. Matching 'abc' anywhere would require no anchor. An exact match would require the expression to be '^abc$', not just '^abc'.

  3. Character Classes

    Which regular expression would match any single digit in a Bash text processing tool?

    1. {0-9}
    2. [0-9]
    3. u003C0-9u003E
    4. (0-9)

    Explanation: The square brackets '[0-9]' define a character class that matches any single digit. Parentheses '(0-9)' are used for grouping in some contexts but do not define a range. Curly braces '{0-9}' are used for repetition and not for character classes. Angle brackets 'u003C0-9u003E' are not standard regex syntax.

  4. Using Sed for Substitution

    Which sed command replaces the first occurrence of 'cat' with 'dog' on each line of 'pets.txt'?

    1. sed 'f/cat/dog/' pets.txt
    2. sed 'r/cat/dog/' pets.txt
    3. sed 'c/cat/dog/' pets.txt
    4. sed 's/cat/dog/' pets.txt

    Explanation: The 's' command in sed stands for substitution, making 'sed 's/cat/dog/' pets.txt' the correct syntax for replacing 'cat' with 'dog'. The 'r', 'c', and 'f' commands do not correspond to substitution and would either cause errors or not perform the intended task.

  5. Grep Case Sensitivity

    How do you use grep to perform a case-insensitive search for the word 'hello' in 'file.txt'?

    1. grep -i 'hello' file.txt
    2. grep -v 'hello' file.txt
    3. grep -n 'hello' file.txt
    4. grep --all 'hello' file.txt

    Explanation: The '-i' option with grep ignores case during the search, making it suitable for case-insensitive matching. '-n' prints line numbers, '-v' inverts the match, and '--all' is not a valid grep flag for this purpose.

  6. Awk Pattern Matching

    Which awk command prints lines from 'data.txt' that contain the pattern 'error'?

    1. awk data.txt { /error/ }
    2. awk '/error/ { print }' data.txt
    3. awk 'error { print }' data.txt
    4. awk print: error data.txt

    Explanation: In awk, patterns can be matched using '/pattern/' inside the command, so 'awk '/error/ { print }' data.txt' correctly prints lines containing 'error'. The second option is invalid syntax, while the third and fourth choices do not conform to awk's command structure.

  7. Wildcard Characters

    In Bash regular expressions, what does the '.' (dot) character represent?

    1. A literal period
    2. Any single character
    3. The end of a line
    4. A whitespace character

    Explanation: The dot (.) is a wildcard character in regular expressions that matches any single character except a newline. It does not signify the end of a line—the dollar sign serves that purpose. To match a literal period, you would need to escape the dot. It does not exclusively match spaces.

  8. Word Boundaries

    Which regular expression would exactly match the whole word 'rain' but not 'training' or 'grain' in grep with extended regular expressions?

    1. #rain#
    2. rain*
    3. <rain>
    4. (rain)

    Explanation: The pattern '<rain>' uses word boundaries to match 'rain' as a full word, excluding words like 'training' or 'grain'. 'rain*' would match 'rai' followed by any number of 'n's. Parentheses alone '(rain)' group expressions but do not enforce word boundaries. '#rain#' is not a recognized syntax for word boundaries.

  9. Counting Matches

    Which command shows the number of lines in 'logs.txt' that contain the text 'fail'?

    1. grep -a 'fail' logs.txt
    2. grep -n 'fail' logs.txt
    3. grep -l 'fail' logs.txt
    4. grep -c 'fail' logs.txt

    Explanation: The '-c' option with grep counts the number of matching lines. '-l' lists file names with matches, '-n' shows line numbers alongside matched lines, and '-a' treats binary files as text, which does not count matches.

  10. Negation in Text Processing

    How can you use grep to display all lines from 'fruit.txt' that do not contain the word 'apple'?

    1. grep -v 'apple' fruit.txt
    2. grep --not 'apple' fruit.txt
    3. grep -x 'apple' fruit.txt
    4. grep -u 'apple' fruit.txt

    Explanation: The '-v' option with grep inverts the match, showing lines that do not contain 'apple'. '--not' is not a valid grep option, '-x' matches whole lines only, and '-u' is related to file processing, not pattern negation.