Hi there
All about "grep" is nice, so saying something like "one of the most useful features of grep ..." would be inaccurate. However, there is one flag that opens up a new dimension on the way we can use "grep". "grep -o" displays only the part of the text which matches the search string.
Code:
pabloa$ echo 'Hello, hello, hello!' > test
pabloa$ grep -o hello test
hello
hello
pabloa$ grep -oi hello test
Hello
hello
hello
This feature combined with regular expressions is extremely powerful. We will have a look at regular expressions next time. In the meantime, the "-o" flag can be used to count the number of occurrences of a specific word in a document (I don't know of another way of doing it, actually). Unfortunately the combination of the "-o" flag with the "-c" flag doesn't work as expected, so we need to do it in two steps, like so:
Code:
pabloa$ grep -oi hello test | wc -l
3
Here "wc" is a UNIX command for counting (characters, words, lines). The "-l" (by the way, this is a lowercase L, not the number 1!) flag says to report only the number of lines (as you'd expect, it's got corresponding "-c" and "-w" flags for characters and words).
Cheers.
P.