====== Grep - Basic usage ====== Assuming a file exists with the following contents: boot book booze machine boots bungie bark aardvark broken$tuff robots ---- ===== Simple Search ===== grep "boo" filename returns: boot book booze boots **NOTE:** grep prints out every line that contains the word **boo**. ---- ===== Simple Search with Line Numbers ===== grep -n "boo" filename returns: 1:boot 2:book 3:booze 5:boots **NOTE:** Using the **-n** option prints includes line numbers. ---- ===== Simple Inverse Search with 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. ---- ===== Only determine how many lines contain the Search String ===== grep -c "boo" filename returns: 4 **NOTE:** grep prints out 4 as there are 4 occurrences of the word **boo**. ---- ===== Determine which files contain the Search String ===== 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. ---- ===== Ignore Case ===== grep -i "BOO" filename **NOTE:** The **-i** option will treat upper and lower case as equivalent while matching the search string. ---- ===== Exact Matches Only ===== 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**. ---- ===== Add additional lines of context ===== 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.