Usage (GNU Grep 3.7)
Next: Performance, Previous: Regular Expressions, Up: grep [Contents][Index]
4 Usage
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.
Here is a more complex example, showing the location and contents of any line containing ‘f
’ and ending in ‘.c
’, within all files in the current directory whose names start with non-‘.
’, contain ‘g
’, and end in ‘.h
’. The -n
option outputs line numbers, the --
argument treats any later arguments as file names not options even if *g*.h
expands to a file name that starts with ‘-
’, and the empty file /dev/null
causes file names to be output even if only one file name happens to be of the form ‘*g*.h
’.
grep -n -- 'f.*\.c$' *g*.h /dev/null
Note that the regular expression syntax used in the pattern differs from the globbing syntax that the shell uses to match file names.
See Invoking grep, for more details about how to invoke grep
.
Here are some common questions and answers about grep
usage.
- How can I list just the names of matching files?
grep -l 'main' test-*.c
How do I search directories recursively?
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 and grep. For example, the following command searches only C files: find /home/gigi -name '*.c' ! -type d \ -exec grep -H 'hello' '{}' +
This differs from the command:
grep -H 'hello' /home/gigi/*.c
which merely looks for ‘hello’ in non-hidden C files in /home/gigi whose names end in ‘.c’. The find command line above is more similar to the command:
grep -r --include='*.c' 'hello' /home/gigi
What if a pattern or file has a leading ‘
-
’?grep -- '--cut here--' *
searches for all lines matching ‘--cut here--’. Without --, grep would attempt to parse ‘--cut here--’ as a list of options, and there would be similar problems with any file names beginning with ‘-’.
Alternatively, you can prevent misinterpretation of leading ‘-’ by using -e for patterns and leading ‘./’ for files:
grep -e '--cut here--' ./*
- Suppose I want to search for a whole word, not a part of a word?
grep -w 'hello' test*.log
searches only for instances of ‘hello’ that are entire words; it does not match ‘Othello’. For more control, use ‘\’ to match the start and end of words. For example:
grep 'hello\>' test*.log
- How do I output context around the matching lines?
grep -C 2 'hello' test*.log
How do I force
grep
to print the name of the file?Append /dev/null:
grep 'eli' /etc/passwd /dev/null
gets you:
/etc/passwd:eli:x:2098:1000:Eli Smith:/home/eli:/bin/bash
Alternatively, use -H, which is a GNU extension:
grep -H 'eli' /etc/passwd
- Why do people use strange regular expressions on
ps
output?ps -ef | grep '[c]ron'
- Why does
grep
report “Binary file matches”? 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, or the -s or --no-messages option. - Why doesn’t ‘
grep -lv
’ print non-matching file names? ‘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. - I can do “OR” with ‘
|
’, but what about “AND”?grep 'paul' /etc/motd | grep 'franc,ois'
- Why does the empty pattern match every input line? 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 ‘^Grep/docs/latest/:blank:*$’. To match no lines at all, use the command ‘grep -f /dev/null’.
How can I search in both standard input and in files?
Use the special file name ‘-’:
cat /etc/passwd | grep 'alain' - /etc/motd
- Why is this back-reference failing?
echo 'ba' | grep -E '(a)\1|b\1'
- How can I match across lines? 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 and output “line” is null-terminated; 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 'fooGrep/docs/latest/:space:\+bar'
- What do
grep
,fgrep
, andegrep
stand for? 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
Next: Performance, Previous: Regular Expressions, Up: grep [Contents][Index]