Here is an example command that invokes GNU grep:
grep -i 'hello.*world' menu.h main.c
This lists all lines in the files menu.h and main.c that contain the string ‘hello’ followed by the string ‘world’; this is because ‘.*’ matches zero or more characters within a line. See Regular Expressions. The -i option causes grep to ignore case, causing it to match the line ‘Hello, world!’, which it would not otherwise match. See Invoking, for more details about how to invoke grep.
Here are some common questions and answers about grep usage.
grep -l 'main' *.c
lists the names of all C files in the current directory whose contents mention ‘main’.
grep -r 'hello' /home/gigi
searches for ‘hello’ in all files under the /home/gigi directory. For more control over which files are searched, use find, grep, and xargs. For example, the following command searches only C files:
find /home/gigi -name '*.c' -print0 | xargs -0r grep -H 'hello'
This differs from the command:
grep -H 'hello' *.c
which merely looks for ‘hello’ in all files in the current directory whose names end in ‘.c’. The ‘find ...’ command line above is more similar to the command:
grep -rH --include='*.c' 'hello' /home/gigi
grep -e '--cut here--' *
searches for all lines matching ‘--cut here--’. Without -e, grep would attempt to parse ‘--cut here--’ as a list of options.
grep -w 'hello' *
searches only for instances of ‘hello’ that are entire words; it does not match ‘Othello’. For more control, use ‘\<’ and ‘\>’ to match the start and end of words. For example:
grep 'hello\>' *
searches only for words ending in ‘hello’, so it matches the word ‘Othello’.
grep -C 2 'hello' *
prints two lines of context around each matching line.
grep 'eli' /etc/passwd /dev/null
Alternatively, use -H, which is a GNU extension:
grep -H 'eli' /etc/passwd
ps -ef | grep '[c]ron'
If the pattern had been written without the square brackets, it would have matched not only the ps output line for cron, but also the ps output line for grep. Note that on some platforms, ps limits the output to the width of the screen; grep does not have any limit on the length of a line except the available memory.
If grep listed all matching “lines” from a binary file, it would probably generate output that is not useful, and it might even muck up your display. So GNU grep suppresses output from files that appear to be binary files. To force GNU grep to output lines even from files that appear to be binary, use the -a or ‘--binary-files=text’ option. To eliminate the “Binary file matches” messages, use the -I or ‘--binary-files=without-match’ option.
‘grep -lv’ lists the names of all files containing one or more lines that do not match. To list the names of all files that contain no matching lines, use the -L or --files-without-match option.
grep 'paul' /etc/motd | grep 'franc,ois'
finds all lines that contain both ‘paul’ and ‘franc,ois’.
The grep command searches for lines that contain strings that match a pattern. Every line contains the empty string, so an empty pattern causes grep to find a match on each line. It is not the only such pattern: ‘^’, ‘$’, ‘.*’, and many other patterns cause grep to match every line.
To match empty lines, use the pattern ‘^$’. To match blank lines, use the pattern ‘^[[:blank:]]*$’. To match no lines at all, use the command ‘grep -f /dev/null’.
Use the special file name ‘-’:
cat /etc/passwd | grep 'alain' - /etc/motd
It can be done by using back-references; for example, a palindrome of 4 characters can be written with a BRE:
grep -w -e '\(.\)\(.\).\2\1' file
It matches the word “radar” or “civic.”
Guglielmo Bondioni proposed a single RE that finds all palindromes up to 19 characters long using 9 subexpressions and 9 back-references:
grep -E -e '^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\9\8\7\6\5\4\3\2\1$' file
Note this is done by using GNU ERE extensions; it might not be portable to other implementations of grep.
echo 'ba' | grep -E '(a)\1|b\1'
This gives no output, because the first alternate ‘(a)\1’ does not match, as there is no ‘aa’ in the input, so the ‘\1’ in the second alternate has nothing to refer back to, meaning it will never match anything. (The second alternate in this example can only match if the first alternate has matched—making the second one superfluous.)
Standard grep cannot do this, as it is fundamentally line-based.
Therefore, merely using the
[:space:] character class does not
match newlines in the way you might expect.
With the GNU grep option -z (--null-data), each input “line” is terminated by a null byte; see Other Options. Thus, you can match newlines in the input, but typically if there is a match the entire input is output, so this usage is often combined with output-suppressing options like -q, e.g.:
printf 'foo\nbar\n' | grep -z -q 'foo[[:space:]]\+bar'
If this does not suffice, you can transform the input before giving it to grep, or turn to awk, sed, perl, or many other utilities that are designed to operate across lines.
The name grep comes from the way line editing was done on Unix. For example, ed uses the following syntax to print a list of matching lines on the screen:
global/regular expression/print g/re/p
fgrep stands for Fixed grep; egrep stands for Extended grep.