0% found this document useful (0 votes)
58 views

GNU Grep Cheatsheet

This document provides a cheatsheet for the GNU grep command. It summarizes grep's basic usage, pattern matching options, output options, context options, and provides examples of common grep commands.

Uploaded by

Alaa Barazi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

GNU Grep Cheatsheet

This document provides a cheatsheet for the GNU grep command. It summarizes grep's basic usage, pattern matching options, output options, context options, and provides examples of common grep commands.

Uploaded by

Alaa Barazi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

6/28/23, 1:59 PM GNU grep cheatsheet

DEVHINTS.IO Edit

Usage
GNU grep cheatsheet Matching opt
grep <options> pattern <file...> -e, --regexp=
-f, --file=FI
-i, --ignore-

Pattern options -v,


-w,
--invert-
--word-re
-x, --line-re
-F, --fixed-strings # list of fixed strings
-G, --basic-regexp # basic regular expression (default)
-E, --extended-regexp # extended regular expression
-P, --perl-regexp # perl compatible regular expression
Expressions
Basic Regular Exp
Output Options In BRE, these c
^ $ . * [ ] \
-c, --count # print the count of matching lines. suppresses normal output
--color[=WHEN] # However, these
applies color to the matches. WHEN is never, always, or auto
-m, --max-count=NUM # stop reading after max count is reached
? + { } | ( )
-o, --only-matching # only print the matched part of a line
-q, --quiet, --silent
-s, --no-messages
Extended Regula
# suppress error messages about nonexistent or unreadable files

ERE gives all of


Context Options ^ $ . * + ? [

Perl Compatible R
PCRE has even
-B NUM, --before-context=NUM # print NUM lines before a match
-A NUM, --after-context=NUM # print NUM lines after a match
-C NUM, -NUM, --context=NUM # print NUM lines before and after a match conditional exp

Examples
# Case insensitive: match any line in foo.txt
# that contains "bar"

https://devhints.io/grep 1/3
6/28/23, 1:59 PM GNU grep cheatsheet
grep -i bar foo.txt

# match any line in bar.txt that contains


# either "foo" or "oof"
grep -E "foo|oof" bar.txt

# match anything that resembles a URL in


# foo.txt and only print out the match
grep -oE "https?://((\w+[_-]?)+\.?)+" foo.txt

# can also be used with pipes:


# match any line that contains "export" in
# .bash_profile, pipe to another grep that
# matches any of the first set of matches
# containing "PATH"
grep "export" .bash_profile | grep "PATH"

# follow the tail of server.log, pipe to grep


# and print out any line that contains "error"
# and include 5 lines of context
tail -f server.log | grep -iC 5 error

0 Comments for this cheatsheet. Write yours!

Search 358+ cheatsheets

Over 358 curated cheatsheets, by developers for developers.


https://devhints.io/grep 2/3
6/28/23, 1:59 PM GNU grep cheatsheet

Devhints home

Other CLI cheatsheets Top cheatsheets

Cron Homebrew Elixir ES2015+


cheatsheet cheatsheet cheatsheet cheatsheet

httpie adb (Android React.js Vimdiff


cheatsheet Debug Bridge) cheatsheet cheatsheet
cheatsheet
Vim Vim scripting
composer Fish shell cheatsheet cheatsheet
cheatsheet cheatsheet

https://devhints.io/grep 3/3

You might also like