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.
Which Bash command with regular expressions would you use to search for lines containing the word 'world' in the file '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.
In a Bash script, what does the regular expression '^abc' match in a line of text?
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'.
Which regular expression would match any single digit in a Bash text processing tool?
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.
Which sed command replaces the first occurrence of 'cat' with 'dog' on each line of '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.
How do you use grep to perform a case-insensitive search for the word 'hello' in '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.
Which awk command prints lines from 'data.txt' that contain the pattern 'error'?
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.
In Bash regular expressions, what does the '.' (dot) character represent?
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.
Which regular expression would exactly match the whole word 'rain' but not 'training' or 'grain' in grep with extended regular expressions?
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.
Which command shows the number of lines in 'logs.txt' that contain the text 'fail'?
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.
How can you use grep to display all lines from 'fruit.txt' that do not contain the word 'apple'?
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.