Grep Command Tutorial For Unix

Tutorials

 

Like any command, grep comes with a handful of options that control both the matches found and the way grep displays the results. The GNU version of grep offers most of the options listed in the following subsections.

Match Control

-e pattern, --regexp=pattern
grep -e -style doc.txt

 

Ensures that grep recognizes the pattern as the regular ex- pression argument. Useful if the regular expression begins with a hyphen, which makes it look like an option. In this case, grep will look for lines that match “-style”.

-f file, --file=file
grep -f pattern.txt searchhere.txt

Takes patterns from file. This option allows you to input all the patterns you want to match into a file, called pattern.txt here. Then, grep searches for all the patterns from pattern.txt in the designated file searchhere.txt. The patterns are additive; that is, grep returns every line that matches any pattern. The pattern file must list one pattern per line. If pattern.txt is empty, nothing will match.

-i, --ignore-case
grep -i 'help' me.txt

 

Ignores capitalization in the given regular expressions, either via the command line or in a file of regular expres- sions specified by the -f option. The example here would search the file me.txt for a string “help” with any iteration of lower- and uppercase letters in the word (“HELP”, “HelP”, etc.). A similar but obsolete synonym to this op- tion is -y.

-v, --invert-match
grep -v oranges filename

 

Returns lines that do not match, instead of lines that do. In this case, the output would be every line in filename that does not contain the pattern “oranges”.

-w, --word-regexp
grep -w 'xyz' filename

 

Matches only when the input text consists of full words. In this example, it is not enough for a line to contain the three letters “xyz” in a row; there must actually be spaces or punctuation around them. Letters, digits, and the underscore character are all considered part of a word; any other character is considered a word boundary, as are the start and end of the line. This is the equivalent of putting

\b at the beginning and end of the regular expression.

-x, --line-regexp
grep -x 'Hello, world!' filename

Like -w, but must match an entire line. This example matches only lines that consist entirely of “Hello, world!”. Lines that have additional content will not be matched. This can be useful for parsing logfiles for specific content that might include cases you are not interested in seeing.

 

General Output Control

-c, --count
grep -c contact.html access.log

 

Instead of the normal output, you receive just a count of how many lines matched in each input file. In the example here, grep will simply return the number of times the contact.html file was accessed through a web server’s ac- cess log.

grep -c -v contact.html access.log

 

This example returns a count of all the lines that do not match the given string. In this case, it would be every time someone accessed a file that wasn’t contact.html on the web server.

--color[=WHEN], --colour[=WHEN]
grep -color[=auto] regexp filename

 

Assuming the terminal can support color, grep will color- ize the pattern in the output. This is done by surrounding the matched (nonempty) string, matching lines, context lines, filenames, line numbers, byte offsets, and separators with escape sequences that the terminal recognizes as color markers. Color is defined by the environment vari- able GREP_COLORS (discussed later). WHEN has three options: never, always, and auto.

-l, --files-with-matches
grep -l "ERROR:" *.log

 

Instead of normal output, prints just the names of input files containing the pattern. As with -L, the search stops on the first match. If an administrator is simply interested in the filenames that contain a pattern without seeing all the matching lines, this option performs that function. This can make grep more efficient by stopping the search as soon as it finds a matching pattern instead of continuing to search an entire file. This is often referred to as “lazy matching.”

-L, --files-without-match
grep -L 'ERROR:' *.log

 

Instead of normal output, prints just the names of input files that contain no matches. For instance, the example prints all the logfiles that contain no reports of errors. This is an efficient use of grep because it stops searching each file once it finds any match, instead of continuing to search the entire file for multiple matches.

-m NUM, --max-count=NUM
grep -m 10 'ERROR:' *.log

 

This option tells grep to stop reading a file after NUM lines are matched (in this example, only 10 lines that contain “ERROR:”). This is useful for reading large files where repetition is likely, such as logfiles. If you simply want to see whether strings are present without flooding the ter- minal, use this option. This helps to distinguish between pervasive and intermittent errors, as in the example here.

-o, --only-matching
grep -o pattern filename

 

Prints only the text that matches, instead of the whole line of input. This is particularly useful when implementing grep to examine a disk partition or a binary file for the presence of multiple patterns. This would output the pat- tern that was matched without the content that would cause problems for the terminal.

-q, --quiet, --silent
grep -q pattern filename

 

Suppresses output. The command still conveys useful in- formation because the grep command’s exit status (0 for success if a match is found, 1 for no match found, 2 if the program cannot run because of an error) can be checked. The option is used in scripts to determine the presence of a pattern in a file without displaying unnecessary output.

-s, --no-messages
grep -s pattern filename

 

Silently discards any error messages resulting from non- existent files or permission errors. This is helpful for scripts that search an entire filesystem without root per- missions, and thus will likely encounter permissions er- rors that may be undesirable. On the other side, it also will suppress useful diagnostic information, which could mean that problems may not be discovered.

In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.