1. Explanation of grep “hanging” behavior (http://www.linuxjournal.com/article/1149):

"If no files are specified on the command line, grep reads standard input. To further illustrate standard input let's try one more example:

$ grep foo

When you run that, grep appears to “hang” waiting for something. It is. It's waiting for input. Type:

tttt
and press return. Nothing happens. Now type:

foobar
and press enter. This time, grep sees the string foo in foobar and echos the line foobar back at you, which is why foobar appears twice. Now type ctrl-d, the “end-of-file” character, to tell grep that it has reached the end of the file, whereupon it exits.

You just gave grep an input file that consisted of tttt, a newline character, foobar, a newline character, and the end-of-file character."

2. Common usage:

grep "search string" filename
grep "search string" *.fileextension
grep "search string" filename1 filename2

3. Switches:
a) print line numbers where match took place:

grep -n "search string" *.fileextension

b) Count how many times the search string is found:

grep -c "search string" *.fileextension
Advertisements