본문 바로가기

SPECIALTY/Linux

grep


grep

 

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 in text editors such as ed.[1] 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.


Usage

This is an example of a common grep usage:

grep apple fruitlist.txt

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.

Regular expressions can be used to match more complicated queries. The following prints all lines in the file that begin with the letter a, followed by any one character, then the letters ple.

grep ^a.ple fruitlist.txt

As noted above, the term "grep" derives from a usage in ed and related text editors. Before grep existed as a separate command, the same effect might have been achieved by doing:

ed fruitlist.txt
g/^a.ple/p
q

where the second line is the command given to ed to print the relevant lines, and the third line is the command to exit from ed.

Like most Unix commands, grep accepts options in the form of command-line arguments, to change many of its behaviors. For example:

grep -i apple fruitlist.txt

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):

grep -w apple fruitlist.txt

But if fruitlist.txt contains apple word followed by hyphen (-) character, it will also get matched.

cat fruitlist.txt
apple
apples
pineapple
apple-
apple-fruit
fruit-apple
 
grep -w apple fruitlist.txt
apple
apple-
apple-fruit
fruit-apple


Variations

There are countless implementations and derivatives of grep available for many operating systems. 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 (and standardized as -E and -F in POSIX[2]). 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 Microsoft Windows platforms provide the find command for simple string searches. Windows includes the "findstr" command which approximates much of the functionality of “grep”. Ports of grep (Cygwin and GnuWin32, for example) are also available for Windows.

Adobe added support for grep in the CS3 version of InDesign. Their support allow for finding and changing the formatting of text or the text itself in a document using grep.[3]


Usage as a conversational verb

In December 2003, the Oxford English Dictionary Online added draft entries for “grep” as both a noun and a verb.

A common verb usage is the phrase “You can't grep dead trees”—meaning one can more easily search through digital media, using tools such as grep, than one could with a hard copy (i.e., one made from dead trees).[4] Compare with google. Visual grep is used as a term meaning to look through text searching for something, in the manner of the grep program.

Applications such as integrated development environments, text editors, and word processors often feature regular expression search, and sometimes refer to it as “grep”.


See also


References

  • Alain Magloire (August 2000). Grep: Searching for a Pattern. Iuniverse Inc. ISBN 0-595-10039-2. 
  • Hume, Andrew A tale of two greps, Software—Practice and Experience 18, ( 11 ), 1063–1072 ( 1988).
  • Hume, Andrew Grep wars: The strategic search initiative. In Peter Collinson, editor, Proceedings of the EUUG Spring 88 Conference, pages 237–245, Buntingford, UK, 1988. European UNIX User Group.


External links

'SPECIALTY > Linux' 카테고리의 다른 글

Makefile 만들기  (0) 2010.03.26
command for search text  (0) 2010.03.17
Homework1  (0) 2010.02.12
Evironment of Linux Server of ESOS Lab.  (0) 2010.02.08
install pear  (0) 2010.02.07