Grep: Difference between revisions
No edit summary |
No edit summary |
||
Line 3: | Line 3: | ||
grep searches the name file or files for the specified string. (You can also use egrep to search inside a file using [[Regular Expressions]] ) | grep searches the name file or files for the specified string. (You can also use egrep to search inside a file using [[Regular Expressions]] ) | ||
=== Case Sensitive === | |||
<code>grep bananas filename.txt</code> This searches for the word '''bananas''' in the file, '''filename.txt''' | <code>grep bananas filename.txt</code> This searches for the word '''bananas''' in the file, '''filename.txt''' | ||
Line 13: | Line 15: | ||
=== String-Search=== | |||
grep searches for strings, not words. It will match partial words, whole words, and that string inside a whole block of text. | |||
So, if I'm looking inside a file for the word "the", and I use: | |||
<code>grep the filename.txt</code> | |||
grep will output all of the following: | |||
- the | |||
- themselves | |||
- theory | |||
- another | |||
(etc...) | |||
... wait, we only wanted "the"... | |||
Fortunately, we can tell grep to do that, like so: | |||
<code>grep -w the filename.txt</code> This will tell grep to only match the word "the". | |||
Revision as of 01:35, 22 December 2016
Usage
grep searches the name file or files for the specified string. (You can also use egrep to search inside a file using Regular Expressions )
Case Sensitive
grep bananas filename.txt
This searches for the word bananas in the file, filename.txt
Note that the search is case sensitive. It will not find Bananas, or BANANAS.
If you want it to find all of those, use:
grep -i bananas filename.txt
String-Search
grep searches for strings, not words. It will match partial words, whole words, and that string inside a whole block of text.
So, if I'm looking inside a file for the word "the", and I use:
grep the filename.txt
grep will output all of the following:
- the - themselves - theory - another (etc...)
... wait, we only wanted "the"...
Fortunately, we can tell grep to do that, like so:
grep -w the filename.txt
This will tell grep to only match the word "the".