Quick Start
Tutorial
Search & Replace
Tools & Languages
Examples
Reference
Regex Tools
grep
PowerGREP
RegexBuddy
RegexMagic
General Applications
EditPad Lite
EditPad Pro
Google Docs
Google Sheets
LibreOffice
Notepad++
Languages & Libraries
Boost
C#
Delphi
F#
GNU (Linux)
Groovy
ICU (Unicode)
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
Python.NET and IronPython
R
RE2
Ruby
std::regex
Tcl
TypeScript
VBScript
Visual Basic 6
Visual Basic (.NET)
wxWidgets
XML Schema
XQuery & XPath
Xojo
XRegExp
Databases
Google BigQuery
MySQL
Oracle
PostgreSQL
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference
Book Reviews
Printable PDF
About This Site
RSS Feed & Blog
RegexBuddy—Better than a regular expression tutorial!

What Is grep?

Grep is a tool that originated from the UNIX world during the 1970’s. It can search through files and folders (directories in UNIX) and check which lines in those files match a given regular expression. Grep will output the filenames and the line numbers or the actual lines that matched the regular expression. All in all a very useful tool for locating information stored anywhere on your computer, even (or especially) if you do not really know where to look.

Using grep

If you type grep regex *.txt then grep searches through all text files in the current folder. It applies the regex to each line in the files, and print (i.e. display) each line on which a match was found. This means that grep is inherently line-based. Regex matches cannot span multiple lines.

If you like to work on the command line then the traditional grep tool makes a lot of tasks easier. All Linux distributions install a version of grep by default, usually GNU grep. If you are using Microsoft Windows then you will need to download and install it separately.

Grep not only works with files, but also with anything you supply on the standard input. When used with standard input, grep prints all lines it reads from standard input that match the regex. The Linux find command prints all file names it finds in a directory, so find | grep regex prints only the file names that match regex.

Grep’s Regex Engine

Most versions of grep use a regex-directed engine, like the regex flavors discussed in the regex tutorial on this website . However, grep’s regex flavor is very limited. On POSIX systems, it uses POSIX Basic Regular Expressions.

An enhanced version of grep is called egrep. It uses a text-directed engine. Since neither grep nor egrep support any of the special features such as lazy repetition or lookaround, and because grep and egrep only indicate whether a match was found on a particular line or not, this distinction does not matter, except that the text-directed engine is faster. On POSIX systems, egrep uses POSIX Extended Regular Expressions. Despite the name “extended”, egrep is almost the same as grep. It just uses a slightly different regex syntax and adds support for alternation, but loses support for backreferences. So it actually has one less feature.

GNU grep, the most popular version of grep on Linux, uses both a text-directed and a regex-directed engine. If you use backreferences then it uses the regex-directed engine. Otherwise, it uses the faster text-directed engine. Again, for the tasks that grep is designed for, this does not matter to you, the user. If you type the “grep” command then you’ll use the GNU Basic Regular Expressions syntax. If you type the “egrep” command then you’ll use the GNU Extended Regular Expressions syntax. The GNU versions of grep and egrep have exactly the same capabilities, including alternation for grep and backreferences for egrep. They only use a slightly different syntax.

Beyond The Command Line

If you like to work on the command line then the traditional grep tool is for you. But if you like to use a graphical user interface, there are many grep-like tools available for Windows and other platforms. Simply search for “grep” on your favorite software download site. Unfortunately, many grep tools come with poor documentation, leaving it up to you to figure out exactly which regex flavor they use. It’s not because they claim to be Perl-compatible, that they actually are. Some are almost perfectly compatible (but never identical, though), but others fail miserably when you want to use advanced and very useful constructs like lookaround.

One Windows-based grep tool that stands out from the crowd is PowerGREP.

Ad-Free Access and Printable PDF Download

If you find the content on this website helpful they you may want a copy you can read offline or even print, or browse the site as often as you want without ads. You can purchase your own copy of the Regular-Expressions.info printable PDF download. As a bonus, you'll get a lifetime of advertisement-free access to this site!

| Quick Start | Tutorial | Search & Replace | Tools & Languages | Examples | Reference |

| grep | PowerGREP | RegexBuddy | RegexMagic |

| EditPad Lite | EditPad Pro | Google Docs | Google Sheets | LibreOffice | Notepad++ |

| Boost | C# | Delphi | F# | GNU (Linux) | Groovy | ICU (Unicode) | Java | JavaScript | .NET | PCRE (C/C++) | PCRE2 (C/C++) | Perl | PHP | POSIX | PowerShell | Python | Python.NET and IronPython | R | RE2 | Ruby | std::regex | Tcl | TypeScript | VBScript | Visual Basic 6 | Visual Basic (.NET) | wxWidgets | XML Schema | XQuery & XPath | Xojo | XRegExp |

| Google BigQuery | MySQL | Oracle | PostgreSQL |