The grep Command - A Regular Expression Search And Extraction Tool

Linux / Unix command "grep" allows you to search for a pattern in a list of files. Such patterns are specified as "regular expressions", which in their simplest form are "strings", such as words or sentence fragments.

The Syntax of Using grep

The way we search for a string with grep is to put the words we are searching for together in single quotes.

  • The syntax: % grep pattern file-name-1 file-name-2 …file-name-n
  • An example: % grep 'mountain bike' sports hobbies

As a result of entering this command the operating system will print all the lines in the file "sports" and the file "hobbies" that contain the string "mountain bike". By default the line will be printed on the computer screen (in the shell window, where the command was issued).

Save Search Results

To save your search to a file, you need to redirect the information in the following way:

  • The syntax: %grep pattern file-name1 file-name2 > results-file
  • An example: % grep 'mountain bike' sports hobbies > results

Here, we saved the results to a file, which we named "results". We can then open this file and look at it either using emacs, vi, or similar programs.

Some Options

The following is a list of some of the most useful grep options:

  • -h if you search more than one file at a time, the results contain the name of the file from which the string was found. This option turns off that feature, giving you only the lines without the file name.
  • -w restricts the search to whole words only
  • -b precedes each matched line with its file block number.
  • -c displays only a count of the number of matched lines and not the lines themselves.
  • -E causes grep to behave like egrep.
  • -e pattern specifies one or more patterns for which grep is to search. You may indicate each pattern with a separate -e option character, or with newlines within pattern. For example, the following two commands are equivalent:
    grep -e pattern_one -e pattern_two file
    grep -e 'pattern_one pattern_two' file
  • -F causes grep to behave like fgrep.
  • -f patternfile reads one or more patterns from patternfile. Patterns in patternfile are separated by newlines.
  • -i - tells grep to ignore case so that it treats "the" and "The" as the same word
  • -l lists only the file names that contain the matching lines.
  • -n - precedes each line with the line number where it was found
  • -q suppresses output and simply returns appropriate return code.
  • -s suppresses the display of any error messages for nonexistent or unreadable files.
  • -U[b|B|l|L] forces the specified files to be treated as Unicode files. By default, these utilities assume that Unicode characters are little-endian. If a byte-order marker is present, that is used to determine the byte order for the characters. You can force Unicode characters to be treated as big-endian by specifying -Ub or -UB. Similarly, you can force them to be treated as little-endian by specifying -Ul or -UL.
  • -v displays all lines not matching a pattern.
  • -x requires a string to match an entire line.

The option (always preceded by a "-") goes between the grep command and the search pattern.

  • The syntax:
    % grep -option pattern file-name1 file-name2 > results-file
  • Example of using a single option:
    % grep -h 'mountain bike' sports hobbies > results
  • Example of using multiple options:
    % grep -hi 'mountain bike' sports hobbies > results

Remember: There can be any number of options, but you should use only one "-".

Two Variations of grep are "fgrep" and "egrep"

fgrep searches files for one or more pattern arguments, but does not use regular expressions. It does direct string comparison to find matching lines of text in the input.

egrep works similarly, but uses extended regular expression matching. If you include special characters in patterns typed on the command line, escape them by enclosing them in apostrophes to prevent inadvertent misinterpretation by the shell or command interpreter. To match a character that is special to egrep, a backslash (\) should be put in front of the character. It is usually easier to use fgrep if you don't need special pattern matching.

Important: Use the man command (% man) to see how a command is used on your particular computer.