grep:basic_usage
This is an old revision of the document!
Table of Contents
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: This is useful if you are searching through multiple files for the same string.
grep/basic_usage.1598093713.txt.gz · Last modified: 2020/08/22 10:55 by 192.168.1.1