Grep: Difference between revisions

From /i/nsurgency W/i/ki
Jump to navigationJump to search
>Maxwell
No edit summary
>Maxwell
No edit summary
Line 1: Line 1:
'''grep''' is a [[command line interface|command line]] text search utility originally written for [[Unix]]. The name is taken from the first letters in ''global'' / ''regular expression'' / ''print'', a series of instructions for the [[ed (software)|ed]] text editor. <ref name="etymology">{{cite web|url=http://www.catb.org/~esr/jargon/html/G/grep.html |title=grep |accessdate=2006-06-29 |author=Raymond, Eric (editor) |last=Raymond |first=Eric |authorlink=Eric S. Raymond |work=Jargon File }}</ref>
'''grep''' is a command line text search utility originally written for Unix. The name is taken from the first letters in global / regular expression / print, a series of instructions for the ed text editor. The grep command searches files or standard input globally for lines matching a given regular expression, and prints them to the program's standard output.
The <code>grep</code> command searches files or [[standard input]] globally for lines matching a given [[regular expression]], and prints them to the program's [[standard output]].


==Usage==
==Download==
This is an example of a common grep usage:
[http://ftp.gnu.mirrors.hoobly.com/gnu/grep/ Download from GNU]
 
<source lang="bash">
grep apple fruitlist.txt
</source>
 
In this case, grep prints all lines containing 'apple' from the file ''fruitlist.txt'', regardless of word boundaries; therefore lines containing 'pineapple' or 'apples' are also printed. The grep command is [[case sensitive]] by default, so this example's output does not include lines containing 'Apple' (with a capital A) unless they also contain 'apple'.
 
Like most Unix commands, grep accepts [[Command-line argument|command line arguments]] to change this and many other behaviors. For example:
 
<source lang="bash">
grep -i apple fruitlist.txt
</source>
 
This prints all lines containing 'apple' regardless of capitalization. The '-i' argument tells grep to be ''case insensitive'', or to ''ignore case''.
 
To print all lines containing 'apple' as a word ('pineapple' and 'apples' will not match):
 
<source lang="bash">
grep -w apple fruitlist.txt
</source>
 
For simplicity, these examples match a single English word, but [[regular expression]]s can be extremely sophisticated (and notoriously difficult to decipher, or [[write-only language|write-only]]).
 
For further details on grep command line arguments and regular expression capabilities/syntax, refer to the particular implementation's documentation.


==Variations==
==Variations==
There are countless implementations and derivatives of grep available for many operating systems, as well as for aiding searches in third-party applications such as EnCase (computer forensic software). Early variants of grep included '''egrep''' and '''fgrep'''. The former applies an extended regular expression syntax that was added to Unix after [[Ken Thompson]]'s original regular expression implementation. The latter searches for any of a list of 'fixed' strings using the [[Aho-Corasick algorithm]]. These variants are embodied in most modern grep implementations as command-line switches (e.g. <code>-E</code> and <code>-F</code> respectively in [[GNU Project|GNU]] grep). In such combined implementations, '''grep''' may also behave differently depending on the name by which it is invoked, allowing '''fgrep''', '''egrep''', and '''grep''' to be links to the same program.
There are countless implementations and derivatives of grep available for many operating systems, as well as for aiding searches in third-party applications such as EnCase (computer forensic software). Early variants of grep included '''egrep''' and '''fgrep'''. The former applies an extended regular expression syntax that was added to Unix after [[Ken Thompson]]'s original regular expression implementation. The latter searches for any of a list of 'fixed' strings using the [[Aho-Corasick algorithm]]. These variants are embodied in most modern grep implementations as command-line switches (e.g. <code>-E</code> and <code>-F</code> respectively in GNU grep). In such combined implementations, '''grep''' may also behave differently depending on the name by which it is invoked, allowing '''fgrep''', '''egrep''', and '''grep''' to be links to the same program.


pcregrep is an implementation of grep that uses [[Regular expression#Perl-derivative regular expressions|Perl regular expression]] syntax.
pcregrep is an implementation of grep that uses Perl regular expression syntax.


Other commands contain the word 'grep' to indicate that they search (usually for regular expression matches). The [[pgrep]] utility, for instance, displays the processes whose names match a given regular expression.
Other commands contain the word 'grep' to indicate that they search (usually for regular expression matches). The pgrep utility, for instance, displays the processes whose names match a given regular expression.


In [[Perl]], grep is a built-in function that finds elements in a list. In [[functional programming]] languages, this [[higher-order function]] is typically named "filter" instead.
In [[Perl]], grep is a built-in function that finds elements in a list. In functional [[programming]] languages, this higher-order function is typically named "filter" instead.


The [[DOS]], [[OS/2]] and [[Microsoft Windows]] platforms provide the [[find (command)|find]] command for simple string searches. Windows also provides the "findstr" command which approximates much of the functionality of “grep”, or you can use the [[cygwin]] grep ported version. A [[GnuWin32]] version of grep is also available.
The DOS, OS/2 and [[||W/i/ndows|Windows]] platforms provide the find command for simple string searches. Windows also provides the "findstr" command which approximates much of the functionality of “grep”, or you can use the cygwin grep ported version. A GnuWin32 version of grep is also available.


==See Also==
==See Also==
[[Unix]]
[[Unix]]
[[DOS]]
 
==External Links==
[http://www.gnu.org/software/grep/ GNUE official page]


[[category:Tools]]
[[category:Tools]]

Revision as of 05:48, 24 March 2009

grep is a command line text search utility originally written for Unix. The name is taken from the first letters in global / regular expression / print, a series of instructions for the ed text editor. The grep command searches files or standard input globally for lines matching a given regular expression, and prints them to the program's standard output.

Download

Download from GNU

Variations

There are countless implementations and derivatives of grep available for many operating systems, as well as for aiding searches in third-party applications such as EnCase (computer forensic software). Early variants of grep included egrep and fgrep. The former applies an extended regular expression syntax that was added to Unix after Ken Thompson's original regular expression implementation. The latter searches for any of a list of 'fixed' strings using the Aho-Corasick algorithm. These variants are embodied in most modern grep implementations as command-line switches (e.g. -E and -F respectively in GNU grep). In such combined implementations, grep may also behave differently depending on the name by which it is invoked, allowing fgrep, egrep, and grep to be links to the same program.

pcregrep is an implementation of grep that uses Perl regular expression syntax.

Other commands contain the word 'grep' to indicate that they search (usually for regular expression matches). The pgrep utility, for instance, displays the processes whose names match a given regular expression.

In Perl, grep is a built-in function that finds elements in a list. In functional programming languages, this higher-order function is typically named "filter" instead.

The DOS, OS/2 and [[||W/i/ndows|Windows]] platforms provide the find command for simple string searches. Windows also provides the "findstr" command which approximates much of the functionality of “grep”, or you can use the cygwin grep ported version. A GnuWin32 version of grep is also available.

See Also

Unix

External Links

GNUE official page