grep

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

VersionRelease Date
POSIX.21992-01-01
IEEE Std 1003.1-20012001-12-06
IEEE Std 1003.1, 2004 Edition2004-01-01
IEEE Std 1003.1, 2013 Edition2013-04-19
IEEE Std 1003.1, 2016 Edition2016-09-30

Illumos/OpenSolaris grep

VersionRelease Date
2005-06-142005-06-14
2005-09-062005-09-06
2012-03-302012-03-30
2012-09-172012-09-17
2013-05-142013-05-14

GNU grep

VersionRelease Date
2.01996-10-01
2.21998-04-27
2.31999-02-14
2.4.12000-03-01
2.4.22000-03-09
2.41999-12-03
2.5.12004-10-29
2.5.1a2004-11-19
2.5.32007-08-02
2.5.42009-02-09
2.52002-03-13
2.6.12010-03-25
2.6.22010-03-29
2.6.32010-04-02
2.62010-03-23
2.72010-09-20
2.82011-05-13
2.92011-06-21
2.102011-11-16
2.112012-03-02
2.122012-04-23
2.132012-07-04
2.142012-08-20
2.152013-10-26
2.162014-01-01
2.172014-02-17
2.182014-02-20
2.192014-05-22
2.202014-06-03
2.212014-11-23
2.222015-11-01
2.232016-02-04
2.242016-03-10
2.252016-04-21
2.262016-10-02
2.272016-12-06
2.282017-02-06

BSD grep / FreeGrep

VersionRelease 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.02014-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

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow