Getting started with grep
Remarks#
grep prints lines that contain a match for a pattern within files.
grep can use regular expressions and has several options to improve the quality of the results.
References
Versions#
POSIX grep
Version | Release Date |
---|---|
POSIX.2 | 1992-01-01 |
IEEE Std 1003.1-2001 | 2001-12-06 |
IEEE Std 1003.1, 2004 Edition | 2004-01-01 |
IEEE Std 1003.1, 2013 Edition | 2013-04-19 |
IEEE Std 1003.1, 2016 Edition | 2016-09-30 |
Illumos/OpenSolaris grep
Version | Release Date |
---|---|
2005-06-14 | 2005-06-14 |
2005-09-06 | 2005-09-06 |
2012-03-30 | 2012-03-30 |
2012-09-17 | 2012-09-17 |
2013-05-14 | 2013-05-14 |
GNU grep
Version | Release Date |
---|---|
2.0 | 1996-10-01 |
2.2 | 1998-04-27 |
2.3 | 1999-02-14 |
2.4.1 | 2000-03-01 |
2.4.2 | 2000-03-09 |
2.4 | 1999-12-03 |
2.5.1 | 2004-10-29 |
2.5.1a | 2004-11-19 |
2.5.3 | 2007-08-02 |
2.5.4 | 2009-02-09 |
2.5 | 2002-03-13 |
2.6.1 | 2010-03-25 |
2.6.2 | 2010-03-29 |
2.6.3 | 2010-04-02 |
2.6 | 2010-03-23 |
2.7 | 2010-09-20 |
2.8 | 2011-05-13 |
2.9 | 2011-06-21 |
2.10 | 2011-11-16 |
2.11 | 2012-03-02 |
2.12 | 2012-04-23 |
2.13 | 2012-07-04 |
2.14 | 2012-08-20 |
2.15 | 2013-10-26 |
2.16 | 2014-01-01 |
2.17 | 2014-02-17 |
2.18 | 2014-02-20 |
2.19 | 2014-05-22 |
2.20 | 2014-06-03 |
2.21 | 2014-11-23 |
2.22 | 2015-11-01 |
2.23 | 2016-02-04 |
2.24 | 2016-03-10 |
2.25 | 2016-04-21 |
2.26 | 2016-10-02 |
2.27 | 2016-12-06 |
2.28 | 2017-02-06 |
BSD grep / FreeGrep
Version | Release Date |
---|---|
OpenBSD 3.0 | 2001-12-01 |
OpenBSD 3.4 | 2003-11-01 |
OpenBSD 3.5 | 2004-05-01 |
OpenBSD 3.6 | 2004-11-01 |
OpenBSD 3.7 | 2005-05-19 |
OpenBSD 3.8 | 2005-11-01 |
OpenBSD 3.9 | 2006-05-01 |
OpenBSD 4.0 | 2006-11-01 |
OpenBSD 4.1 | 2007-05-01 |
OpenBSD 4.3 | 2008-05-01 |
OpenBSD 4.8 | 2010-11-01 |
OpenBSD 5.0 | 2011-11-01 |
OpenBSD 5.3 | 2013-05-01 |
OpenBSD 5.7 | 2015-05-01 |
OpenBSD 5.8 | 2015-10-18 |
OpenBSD 5.9 | 2016-03-29 |
NetBSD 2.0 | 2004-12-09 |
NetBSD 4.0 | 2007-12-19 |
NetBSD 6.0 | 2012-10-17 |
NetBSD 7.0 | 2015-09-25 |
FreeBSD 9.0 | 2012-01-02 |
FreeBSD 10.0 | 2014-01-16 |
Basic usage
Ignore case
Given a file sample
:
hello
Hello
HELLO_there
A normal grep
for “hello” returns:
$ grep "hello" sample
hello
Using -i
allows to ignore case and match any “hello”:
$ grep -i "hello" sample
hello
Hello
HELLO_there
Match whole words
Given a file sample
:
hello world
ahello here
hello_there
A normal grep
for “hello” returns:
$ grep hello sample
hello world
ahello here
hello_there
Using -w
allows to select those lines containing matches that form whole words:
$ grep -w hello sample
hello world
Find text within a given directory, recursively
Using GNU grep
grep -r 'pattern' <directory path>
To also list line numbers of matches use -n
option
grep -rn 'pattern' <directory path>
To search only files with particular glob pattern
grep --include='*.txt' -r 'pattern' <directory path>
Exclude file patterns or directories
grep -R --exclude=*.log 'pattern' <directory path>
grep -R --exclude={*.log,*.class} 'pattern' <directory path>
grep -R --exclude-dir=tmp 'pattern' <directory path>
grep -R --exclude-dir={tmp,lib} 'pattern' <directory path>
Notes and other useful options
<directory path>
can be skipped if searching in current directory- The
-R
options follows all symbolic links, unlike-r
which follows symbolic links only if they are on the command line -l
to only list matching files-h
to suppress filename prefix--color=auto
to highlight matched patterns-m <num>
to specify maximum number of matches for each file input
POSIX workaround to search recursively
find <directory path> -type f -exec grep -l 'pattern' {} +
- Options like
-n
,-l
, etc can be used as required - If
{} +
is not supported, use{} \;
instead - See find documentation for more help on
find
command like how to include/exclude file types, directories etc
Prints only the matching part of the lines
echo "Prints only the matching part of the lines" | grep -o "matching"
# prints matching
Grep Context Control
Given a file Sample called movieslist.
Troy
Gladiator
Robin Hood
King Arthur
BraveHeart
The Last Samurai
Normal grep returns
grep "Gladiator" movieslist
Gladiator
Now,using grep to print the below or above lines of the file.
To print the below line
grep -A 1 Gladiator movieslist
Gladiator
Robin Hood
To print the above line
grep -B 1 Gladiator movieslist
Troy
Gladiator
To print both
grep -C 1 Gladiator movieslist
Troy
Gladiator
Robin Hood