How do I count lines if given word or string matches for each input file under Linux or UNIX operating systems?
You need to pass the -c or --count option to suppress normal output. It will display a count of matching lines for each input file:
$ grep -c vivek /etc/passwd
OR
$ grep -w -c vivek /etc/passwd
Sample outputs:
1
However, with the -v or --invert-match option it will count non-matching lines, enter:
$ grep -c vivek /etc/passwd
Sample outputs:
45
You should follow me on twitter here or grab rss feed to keep track of new changes.
This FAQ entry is 4 of 7 in the "Linux / UNIX grep Command Tutorial" series. Keep reading the rest of the series:











{ 2 comments… read them below or add one }
dude, typo again:
instead of
grep -c vivek /etc/passwd
it must be, as correctly described
grep -v vivek /etc/passwd
Greets, Jan
dude, typo again:
instead of
grep -c vivek /etc/passwd
it must be, as correctly described
grep -c -v vivek /etc/passwd
:)
Greets, Jan