You are previewing grep Pocket Reference.

grep Pocket Reference

Cover of grep Pocket Reference by John Bambenek... Published by O'Reilly Media, Inc.
O'Reilly logo

Basic Regular Expressions (grep or grep -G)

This section focuses on basic grep. Most of the flags for basic grep apply equally to the other versions, which we’ll discuss later.

Basic grep, or grep -G, is the default pattern matching type that is used when calling grep. grep interprets the given set of patterns as a basic regular expression when it executes the command. This is the default grep program that is called, so the -G option is almost always redundant.

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 expression 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 ...

The best content for your career. Discover unlimited learning on demand for around $1/day.