Assuming a file exists with the following contents:
boot book booze machine boots bungie bark aardvark broken$tuff robots
grep "boo" filename
returns:
boot book booze boots
NOTE: grep prints out every line that contains the word boo.
grep -n "boo" filename
returns:
1:boot 2:book 3:booze 5:boots
NOTE: Using the -n option prints includes line numbers.
grep -vn "boo" filename
returns:
4:machine 6:bungie 7:bark 8:aaradvark 9:robots
NOTE: Using the -v option prints the inverse of the search string.
This is the items not found.
grep -c "boo" filename
returns:
4
NOTE: grep prints out 4 as there are 4 occurrences of the word boo.
grep -l "boo" *
NOTE: The -l option prints only the filenames of files in the query that have lines that match the search string.
This is useful if you are searching through multiple files for the same string.
grep -i "BOO" filename
NOTE: The -i option will treat upper and lower case as equivalent while matching the search string.
grep -x "boo" filename
NOTE: The -x option looks for eXact matches only.
The result in this example will print nothing, because there are no lines that only contain the pattern boo.
grep -A2 "mach" filename
returns:
machine boots bungie
NOTE: The A option will print out the search string plus a number of additional lines.
In this example 2 additional lines.