Extract lines beginning with WARN: or ERROR: from log.txt.
Exclude INFO: and ERROR: that occur later in a line.
Which ERE is correct?
Grouping the alternatives makes the ^ anchor apply to both WARN and ERROR, while the colon remains required after either word.
Detailed explanation
^WARN|ERROR:Incorrect. The anchor applies only to WARN, so ERROR: could occur later in a line.
Incorrect. The anchor applies only to WARN, so ERROR: could occur later in a line.
^(WARN:ERROR):Incorrect. It requires the literal combined text WARN:ERROR rather than either alternative.
Incorrect. It requires the literal combined text WARN:ERROR rather than either alternative.
^(WARN|ERROR):Correct. The grouped alternation is anchored and followed by a colon.
Correct. The grouped alternation is anchored and followed by a colon.
^[WARN|ERROR]:Incorrect. A bracket expression matches one character from a set; it does not implement word alternatives.
Incorrect. A bracket expression matches one character from a set; it does not implement word alternatives.
Try it yourself
An example you can run in a temporary verification environment.
printf '%s\n' 'WARN: disk' 'ERROR: net' 'INFO: ERROR: old' | grep -E '^(WARN|ERROR):'Expected result
WARN: disk
ERROR: netKey points
- | expresses alternation
- Parentheses define its scope
- Check what the anchor applies to
Notes
- Environment: GNU grep 3.x / LC_ALL=C
- 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.