grep command in UNIX is a command line utility for printing lines that match a pattern. It can be used to find text in a file and search a directory structure of files recursively. It also supports showing the context of a match by showing lines before and after the result and has support for regular expressions in pattern matching.
To find text in a file pass the string you are looking for to
grep followed by the name of the file or files.
grep 'computer' /usr/share/dict/words computer
grep tool will print occurrences that it finds to standard output.
To list line numbers and file names pass the
-n option to grep. This prints matches to standard output along with the line number it was found on.
grep 'computer' -n /usr/share/dict/words 40565
This can be useful if you are looking to edit a file and want to launch vim and go straight to the line.
vim +40565 /usr/share/dict/words
To print lines before and after a match the
-B options can be used. Both expect a number and will print this number of lines.
grep -B 2 -A 2 'computer' /usr/share/dict/words computativeness compute computer computist computus
-B options can be very useful for grepping through log files to see what occurred before and after the item of interest.
A further option is available in
-C that will print the context of the match. This is equivalent to using both
grep -C 2 'computer' /usr/share/dict/words computativeness compute computer computist computus
--context option may also be used and defaults to two lines before and after if no number is given.
To count the number of matches use the
-c option. This outputs a number count to standard output.
grep -c 'comput*' /usr/share/dict/words 50
To print the filename for a match use the
-H option. This is automatically invoked when
grep is given more than one file to search.
grep -H 'computer' /usr/share/dict/words /usr/share/dict/words:computer
To search for a pattern recursively use the
-R option. This will search through all files in the directory tree that you have permission to read.
grep -R 'passwd' /etc /etc/pam.d/su:# NIS (man nsswitch) as well as normal /etc/passwd and /etc/pam.d/chpasswd:# The PAM configuration file for the Shadow 'chpasswd' service
To search for the inverse of a pattern use the
-v option. This will print inverse matches to standard output.
grep -v 'computer' /usr/share/dict/words A a aa aal ....
To ignore case when searching use the
-i option. By default
grep will respect case.
grep 'COMPUTER' /usr/share/dict/words # no match grep -i 'COMPUTER' /usr/share/dict/words computer
To use basic regular expressions all versions of
grep support basic character matches. In the following example the pattern matches ‘ia’ characters at the end of the line.
grep 'ia$' /usr/share/dict/words abasia Abelia abepithymia ....
A great book for understanding the power of regular expressions is Mastering Regular Expressions.
To use extended regular expressions use the
-e option. The following line matches lines that do not contain the words ‘foo’ or ‘bar’.
grep -v -e 'foo' -e 'bar'
Note that in the GNU version of
grep there is no difference in available functionality between basic and extended syntaxes.
Have an update or suggestion for this article? You can edit it here and send me a pull request.
Build your own Vim statusline
Statuslines in Vim are not hard to create. Making your own means one less dependency in your life.
Custom Vim Bindings in tmux 2.4
tmux 2.4 made a significant change to key bindings. Here is how to support custom keybindings for versions before and after tmux 2.4
Using template files in Vim
Vim templates or skeletons, allow you to specify a template to be used for new files with a certain extension.