User Tools

Site Tools


grep:basic_usage

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
grep:basic_usage [2020/08/22 10:46] – created 192.168.1.1grep:basic_usage [2020/08/22 11:02] (current) 192.168.1.1
Line 19: Line 19:
 ---- ----
  
-===== Simple search =====+===== Simple Search =====
  
 <code bash> <code bash>
Line 39: Line 39:
  
 ---- ----
 +
 +===== Simple Search with Line Numbers =====
 +
 +<code bash>
 +grep -n "boo" filename
 +</code>
 +
 +returns:
 +
 +<code bash>
 +1:boot
 +2:book
 +3:booze
 +5:boots
 +</code>
 +
 +<WRAP info>
 +**NOTE:**  Using the **-n** option prints includes line numbers.
 +</WRAP>
 +
 +----
 +
 +===== Simple Inverse Search with Line Numbers =====
 +
 +<code bash>
 +grep -vn "boo" filename
 +</code>
 +
 +returns:
 +
 +<code bash>
 +4:machine
 +6:bungie
 +7:bark
 +8:aaradvark
 +9:robots
 +</code>
 +
 +<WRAP info>
 +**NOTE:**  Using the **-v** option prints the inverse of the search string.
 +
 +This is the items not found.
 +</WRAP>
 +
 +----
 +
 +===== Only determine how many lines contain the Search String =====
 +
 +<code bash>
 +grep -c "boo" filename
 +</code>
 +
 +returns:
 +
 +<code bash>
 +4
 +</code>
 +
 +<WRAP info>
 +**NOTE:**  grep prints out 4 as there are 4 occurrences of the word **boo**.
 +</WRAP>
 +
 +----
 +
 +===== Determine which files contain the Search String =====
 +
 +<code bash>
 +grep -l "boo" *
 +</code>
 +
 +<WRAP info>
 +**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.
 +</WRAP>
 +
 +----
 +
 +===== Ignore Case =====
 +
 +<code bash>
 +grep -i "BOO" filename
 +</code>
 +
 +<WRAP info>
 +**NOTE:**  The **-i** option will treat upper and lower case as equivalent while matching the search string.
 +</WRAP>
 +
 +----
 +
 +===== Exact Matches Only =====
 +
 +<code bash>
 +grep -x "boo" filename
 +</code>
 +
 +<WRAP info>
 +**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**.
 +</WRAP>
 +
 +----
 +
 +===== Add additional lines of context =====
 +
 +<code bash>
 +grep -A2 "mach" filename
 +</code>
 +
 +returns:
 +
 +<code bash>
 +machine
 +boots
 +bungie
 +</code>
 +
 +<WRAP info>
 +**NOTE:**  The **A** option will print out the search string plus a number of additional lines.
 +
 +In this example 2 additional lines.
 +</WRAP>
 +
  
grep/basic_usage.1598093197.txt.gz · Last modified: 2020/08/22 10:46 by 192.168.1.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki