What is the difference between `grep -c` and `grep | wc -l`, and between `grep -o | wc -l`?
IntermediateConceptText processing: grep, sed, awk, cut, sort, uniq
Answer
grep -c ERROR f and grep ERROR f | wc -l both count lines that match. grep -o ERROR f | wc -l counts occurrences, since a line with two matches prints two lines. grep -c is a single process and returns exit status 1 when the count is 0, which matters under set -e:
errors=$(grep -c ERROR app.log || true)
More on Text processing: grep, sed, awk, cut, sort, uniq
What are the most useful `grep` flags?BeginnerWhat is the difference between basic, extended and Perl regular expressions in grep?IntermediateWhat are the core `sed` commands you use in automation?BeginnerWhy does `sed -i` behave differently on macOS and Linux?IntermediateHow do you use variables inside a `sed` expression safely?AdvancedWhat is the structure of an `awk` program?Beginner