[grep] Count matching lines

Created on November 30, 2026

Use -c to count lines containing a match. Each matching line adds one to the count, even if the pattern occurs on that line more than once.

printf 'apple\norange\napple\n' | grep -c 'apple'

Output

2
Shell command grep