grep: Usage

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