from Wikipedia, the free encyclopedia
GNU grep

grep [ gɹɛp ] is a program that is used to search for and filter defined character strings from files or data streams under the Unix and Unix derivatives operating systems . It was originally developed by Ken Thompson , first appeared as part of Version 6 AT&T UNIX, and is POSIX.1- compliant.

The term grep stands for g lobal / r egular e xpression / p rint or g lobal search for a r egular e xpression and p rint out matched lines , or about "global search for a regular expression and output matching lines". Historically, the name developed from the command g / re / p of the Unix editor QED, the predecessor of ed .

There are several other programs with similar functions, such as agrep and tre-agrep , which also allow a fuzzy search for text strings .


In the command line, grep is mostly used to search files. The call has the form:

  grep [optionen] Suchstring [Datei(-liste)]

The command can also be used in shell scripts , for example in pipes .

By command line switches the mode of working, of grep be changed.

Examples of optional ways of working with grep
counter effect
-v Find all lines that do not contain the specified text pattern.
-w Find only lines that contain the search pattern as a whole word.
-n Output the line numbers in which the text is contained.
-H Print the filename of all files that contain the specified text.

The GNU variant also offers the option of not outputting the entire line in which an expression occurs, but only all occurrences of the expression itself.

The example extracts a number, namely the user ID of an account named "rudi" from the file "/ etc / passwd".

$ grep -E -o "^rudi:x:[0-9]+" /etc/passwd | grep -o "[0-9]*$"

Areas of application

grep is primarily used when files have to be searched, for example the source files of a computer program or log files . A whole tree of files can be read in cooperation with find .

$ find /home/benutzername -exec grep -H "Passwort" {} \;

This command searches all files in the source directory of the user username for the string password and displays the lines found together with the file name, the display of the file name being triggered by the -H option.

Some grep versions allow a shorter formulation using the -r option:

$ grep -r "Passwort" /home/benutzername

A second field of application is the use as a line filter within a chain of commands, e.g. B .:

$ tail -1000 /var/log/mail | grep "Mailadresse"


agrep ( Approximate GREP ) is a program that does not belong to the family of UNIX grep programs. Unlike the programs of the UNIX grep family enables a fuzzy ( fuzzy ) text string search.

agrep was developed in the years 1988–1991 by Udi Manber and Sun Wu at the University of Arizona and forms the core of the search engine GLIMPSE and HARVEST . From a large number of built-in search algorithms, the program uses the one that is best adapted to the regular expression of the user input (search string) and that enables the fastest search.

Around 1996 it was ported for the first time to OS / 2 and DOS , later to Windows , and after further improvements back to Linux.

While agrep could previously only be used under a restricted license, which was in conflict with paragraph 1 of the open source definition, the developers managed to place agrep under the free ISC license on September 18, 2014 .


With Tre-agrep, a new implementation of agrep is available, which is available under the more liberal BSD license.


egrep (Extended grep) is suitable for an easier search with extended regular expressions . The characters ? + { } | ( )in egrep have a special meaning for the expression and must be protected with a backslash if they are to be found as text.

grep -Edoes everything that egrep did, and the call egrepis now considered out of date; it should be avoided in scripts, as there is egrepoften only an indirect call to grep -Eand may no longer be supported in the future.


fgrep ( f  for fixed-strings ) can always be used instead of grep if the pattern to be searched for does not contain any regular expressions. All special characters in the pattern string lose their special meaning and are understood as part of the pattern. Fgrep works a bit faster than grep and is particularly useful when searching through large amounts of data. The Aho-Corasick algorithm , which was also used in the original version, is used for this. The call fgrepshould be avoided in scripts, since fgrep often only exists for reasons of downward compatibility and is then usually only an indirect, i.e. detour, call from grep -F.

Microsoft Windows

As with many programs, grep has been ported to the Windows platform. Windows knows the commands find and findstr as built-in commands , which perform a function similar to grep. The Resource Kit 2003 Tools includes QGREP.EXE .

The corresponding PowerShell command is Select-String .

See also

Web links

Individual evidence

  1. ^ Brian Kernighan : A Regular Expression Matcher .
  2. grep manual point 16
  3. J. Plate: unix5 FH Munich
  4. Linux primer
  5. ^ Félix López, Víctor Romero: Mastering Python Regular Expressions. Packt Publishing 2014, ISBN 978-1-78328-315-6 . P. 7.
  6. Computerphile: Where GREP Came From - Computerphile. July 6, 2018, accessed on July 8, 2018 (English, interview with Brian W. Kernighan ).
  7. Udi Manber in the English language Wikipedia
  8. agrep source code of all versions
  9. Details and further information in the English-language agrep article
  10. Charly Kühnast: Take two: From the everyday life of a sysadmin: Biabam and Tre-agrep ., issue 10/2012, p. 61
  11. Information on Tre-agrep
  12. Resource Kit Tools Windows Server 2003 (also for Windows XP )
  13. sdwheeler: Select-String (Microsoft.PowerShell.Utility). In: July 6, 2018, Retrieved July 7, 2018 (American English).