Grep command searches file contents to find lines that contain a pattern. The pattern is described in Regular Expression (regex) language. Here are three youtube video links about Grep and regular expressions.
Ryan's Tutorials website has a nice introduction
to Grep and Regular Expression. Note egrep is a shortcut to
grep -E in
Ubuntu. The man page of grep describes that "in GNU grep there is no difference
in available functionality between basic and extended syntaxes".
$which egrep /bin/egrep $file /bin/egrep /bin/egrep: POSIX shell script, ASCII text executable $vim /bin/egrep # file content is #!/bin/sh exec grep -E "$@"
Here is a very simple grep example:
$grep "Jane Williams" filename.txt
The grep command has many options. A few common ones are:
filename.txt in the above example could be either
For example, this command will search current directory and subdirectories.
$grep -winr "Jane Williams" ./
Here are a few more options:
You can use pipes to search the results of another command.
$history | grep "git commit"
-P option interprets the pattern as a Perl-compatible regex. The
Python and Perl "essentially has the same regex syntax". So with the
option turned on, the grep command will accept Python style special characters
in pattern. Here is a list of common regex special characters in Python: