“grep” is a unix command line tool to search a file (or files) for lines containing a match to the given pattern (often a regular expression). Its name comes from the ed command g/re/p for globally search a regular expression and print (1). See the grep manual.
The basic syntax is:
grep [option(s)] pattern [file(s)]
Options can be omitted and ‘file’ will default to standard input if omitted.
Pattern
First, let’s start with the pattern, the only non-optional part of the command.
The pattern can simply be a plain old string. For example:
grep the
searches standard input for lines containing the.
Frequently, you grep for patterns using regex (regular expressions). Some examples:
- grep ‘^the’ //only find lines starting with the
- grep ‘the$’ //only find lines ending with the
Files
More commonly, grep is used with files. Given this file called example.txt:
the quick brown
brown fox jumped
jumped over these lazy
lazy dog
running this command:
grep the example.txt
would return:
the quick brown
jumped over these lazy
Multiple files
I personally use grep most for searching multiple files.
To search all files in the current directory:
grep “txtToSearchFor” *
To search for all files in the current directory and all sub directories:
grep -r “txtToSearchFor” .
The –include=and –exclude options limit the files searched. For example, the following command will list all java files (in the current and all sub directories) that contain MyClassName:
grep -rl –include=*.java “MyClassName” .
And this command does something similar, but ignores .class and .jar files:
grep -rl –exclude=*.{jar,class} “MyClassName” .
Finally, this example uses regex, and would for example return all java and txt files that contain both MyClassName and MyFileName:
grep -rl –include=*.{java,txt} “My.*Name” .
For more examples of finding all files containing a text string, see this stackoverflow posting.
Multiple strings in multiple files
You can also search for files that contain several strings in a file. For example
grep -rl –include=*Test.java Str1 . | xargs grep -l Str2
This will find all java files that contain both Str1 and Str2.
See more at http://stackoverflow.com/questions/4795323/grep-for-multiple-strings-in-file-on-different-lines-ie-whole-file-not-line-b
Options
Some useful options:
- -n show line number
- -w match the whole word only
- -i ignore case
- -r recursively search a directory
- -l show the file name, not the individual matching line
So again using the above example file, running this command:
grep -inw THE example.txt
(i.e. ignore case, show lines numbers and match whole words only) would return:
1: the quick brown
Alternatives
There are a couple of alternatives to grep, that I know of: ack and ag (aka the silver searcher). Both seem to be significantly faster than grep, and more focussed on searching source code.
For me personally, I will try to get more familiar with the basics of grep first. Grep has the advantage of being available on almost every installation, and widely documented and understood. I have also found that by using the –include flag and avoiding the -i (ignore case) flag, performance increases drastically.