Find lines in events.log beginning with error:, ERROR:, Error:, or another case variation.
Ignore occurrences in the middle of a line.
Which command is appropriate?
grep -i ignores letter case while preserving the ^ anchor, so only a case-insensitive line-start prefix matches.
Detailed explanation
grep -v '^error:' events.logIncorrect. -v selects nonmatches and still treats case as distinct.
Incorrect. -v reverses the selection and therefore keeps nonmatching lines.
grep -n '^error:' events.logIncorrect. -n adds line numbers but does not change case sensitivity.
Incorrect. -n adds line numbers but does not make the comparison case-insensitive.
grep -F '^error:' events.logIncorrect. -F makes ^ literal rather than an anchor.
Incorrect. -F treats the caret literally and still distinguishes letter case.
grep -i '^error:' events.logCorrect. -i ignores case and ^ keeps the match at the line start.
Correct. -i ignores case while the caret keeps the prefix at the line start.
Try it yourself
An example you can run in a temporary verification environment.
printf 'ERROR: disk\nError: net\nINFO: error: old\n' | grep -i '^error:'Expected result
ERROR: disk
Error: netKey points
- -i is case-insensitive
- ^ anchors the line start
- Middle-of-line matches are excluded
Notes
- Environment: GNU grep 3.x / C.UTF-8ロケール
- Use temporary files and harmless commands for pattern experiments.
- Regular-expression details should be checked against the target GNU implementation.
Foundation review
Place the condition
The case rule and the line-position rule are independent and can be combined in one grep command.
Check the result
Test ERROR, Error, and an indented occurrence in one input.