Grep
From DssWiki
grep is a command line utility that was originally written for use with the Unix operating system. Given a list of files or standard input to read, grep searches for lines of text that match one or many regular expressions, and outputs only the matching lines.
Contents |
[edit] Function and usage
The name, "grep", derives from the command used to perform a similar operation, using the Unix text editor ed:
g/re/p
which means "search globally for lines matching the regular expression, and print them". There are various command line switches available when using grep that modify this default behavior including printing lines which do not match, finding or excluding files to search, and annotating the output in various ways. There are also multiple, modern implementations of the classic Unix grep, each with a unique feature set.
This is an example of a common grep usage:
grep apple fruitlist.txt
Grep would return, in this case, all of the lines in file fruitlist.txt with an instance of apple in them. Keep in mind that grep would not return lines with Apple (capital A) because by default grep is case sensitive. Like most Unix commands grep accepts flags which can be used to change this and many other behaviors. For example:
grep -i apple fruitlist.txt
This would return all lines with the words 'apple', 'Apple', 'apPLE', or any other mixing of capital and lower case.
The option -e can also be used to declare multiple patterns for searching on the command line with some versions of grep.
[edit] Variations
There are many derivatives of grep, for example agrep which stands for approximate grep to facilitate fuzzy string searching, fgrep for fixed pattern searches, and egrep for searches involving more sophisticated regular expression syntax. fgrep and egrep are typically the same program as grep, which behaves differently depending on the name by which it is invoked. Tcgrep is a rewrite of grep which uses Perl regular expression syntax. All these variations of grep have been ported to many computer operating systems.
Many other commands contain the word "grep." pgrep, for instance, displays the processes whose names match a regular expression.
In Perl, grep is a built-in function, which when provided both a regular expression (or a general code block) and a list, returns the elements of that list matched by the expression. In functional programming languages, this higher-order function is typically named "filter" instead.
The Microsoft Windows platform provides a utility called "findstr" which approximates much of the functionality of "grep."
[edit] Usage as a conversational verb
As the name "grep" neatly fits the phonology of English, it is often used as a verb, meaning to search – usually, to search a known set of files, as one would with the grep utility. The direct object is the set of files searched: "Kibo grepped his Usenet spool for his name." Compare with google. Sometimes visual grep is used as a term meaning to look through text searching for something, in the manner of the grep program.
In December 2003, the Oxford English Dictionary Online added draft entries for "grep" as both a noun and a verb.
A common usage is the phrase "You can't grep dead trees" - meaning computerized documentation is better than printed documentation (paper is made from dead trees) because computers can search documents by using tools such as grep.
The word "grep" has also become a synonym for regular expressions themselves. Many text and word processors now employ regular expression search features, which those applications will often refer to as a "grep tool" or "grep mode" in which one creates "grep patterns", causing confusion, especially in non-Unix environments.
[edit] egrep and fgrep
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 simply reads a set of fixed strings to search for from a file. These early variants are embodied in most modern grep implementations as simple command-line switches (e.g. -E and -F respectively in GNU grep).
Shell script grep returns lines above and below grep line:
#! /bin/sh
if [ $# -lt 2 ]; then
echo "Usage: wgrep <phrase> <window size> <file>"
exit
fi
phrase=$1
window=$2
file=$3
linenumbs=`grep -n $phrase $file | awk -F: '{ print $1 }'`
for lnumber in $linenumbs; do
before=`expr $lnumber - $window`
if [ $before -lt 1 ] ; then
current=1
else
current=$before
fi
after=`expr $lnumber + $window`
while [ $current -le $after ]; do
if [ $current -eq $lnumber ]; then
/usr/ucb/echo -n " "
fi
sed -n ${current}p $file
current=`expr $current + 1`
done
done
[edit] See also
- List of Unix programs
- Boyer-Moore string search algorithm
- searchmonkey alternative search tool using the Gtk front-end
[edit] References
- cite book|title=Grep: Searching for a Pattern|author=Alain Magloire|publisher=Iuniverse Inc|year=August, 2000|id=ISBN 0-595-10039-2
- Andrew Hume. 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.
- 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
[edit] External links
- GNU grep - the grep that usually comes with Linux distributions
- GNU grep manual page
- GNU grep texinfo documentation
- Grep for Unix/Linux
- AIX grep reference
- "The Treacherous Optimization" -(article on tradeoffs in grep to favor best-case over worst-case scenarios)
- source for Tcgrep
- James Howard's FreeGrep, under a BSD License
- Perl's grep
- Egrep for linguists An introduction to egrep.
- Grep for Windows
- Tony Abou-Assaleh's list of Greps
- agrep Approximate Grep
- cgrep Context Grep
- sgrep Structured Grep
- nr-grep nondeterministic reverse grep
- grouse grep
- grep.py Grep in Python
{{#if: |
Unix command line programs and builtins (more) | |||
| File and file system management: | cat | chattr | cd | chmod | chown | chgrp | cksum | cmp | cp | du | df | file | fsck | fuser | ln | ls | lsof | mkdir | mount | mv | pwd | rm | rmdir | split | touch | ||
| Process management: | at | chroot | crontab | exit | kill | killall | nice | pgrep | pidof | pkill | ps | sleep | time | top | wait | watch | ||
| User Management/Environment: | env | finger | id | mesg | passwd | su | sudo | uname | uptime | w | wall | who | whoami | write | ||
| Text processing: | awk | comm | cut | ed | ex | fmt | head | iconv | join | less | more | paste | pg | sed | sort | tac | tail | tr | uniq | wc | xargs | ||
| Shell programming: | basename | echo | expr | false | printf | test | true | unset | Printing: | lp |
| Communications: inetd | netstat | ping | rlogin | traceroute | Searching: find | grep | strings | Miscellaneous: banner | bc | cal | dd | man | size | yes | |
