Extract lines beginning with CRITICAL: or ALERT: from alerts.log.
Use GNU grep extended regular expressions.
Which command is appropriate?
grep -E enables extended regular expressions, where (A|B) expresses a choice between alternatives.
Detailed explanation
grep -E '^(CRITICAL|ALERT):' alerts.logCorrect. -E makes the grouped alternation work and ^ anchors it at the line start.
Correct. -E makes the grouped alternation work and ^ anchors it at the line start.
grep '^(CRITICAL|ALERT):' alerts.logIncorrect. Basic grep does not treat unescaped parentheses and | as this alternation.
Incorrect. Basic grep does not treat unescaped parentheses and | as this alternation.
grep -F '^(CRITICAL|ALERT):' alerts.logIncorrect. -F treats the metacharacters as literal text.
Incorrect. -F treats the metacharacters as literal text.
grep '^[CRITICAL|ALERT]:' alerts.logIncorrect. Brackets form a one-character class, not alternatives of whole words.
Incorrect. Brackets form a one-character class, not alternatives of whole words.
Try it yourself
An example you can run in a temporary verification environment.
printf 'INFO: ok\nALERT: load\nCRITICAL: disk\n' | grep -E '^(CRITICAL|ALERT):'Expected result
ALERT: load
CRITICAL: diskKey points
- -E selects ERE
- | expresses alternation
- Parentheses group the alternatives
Notes
- Environment: GNU grep 3.x
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.