Skip to content

Instantly share code, notes, and snippets.

@bbrother92
Last active June 27, 2022 11:29
Show Gist options
  • Save bbrother92/64c22b8be8221b45709ae1c03cd25028 to your computer and use it in GitHub Desktop.
Save bbrother92/64c22b8be8221b45709ae1c03cd25028 to your computer and use it in GitHub Desktop.
#bash #regex #cheatsheet #grep

GREP Options

--max-count=number of matches before stopping search
--exclude=*.txt  with -r option

Note: Lookahead and lookbehind are Perl-style regular expression elements, so you'd have to use Perl directly, or GNU grep with the -P option, which then interprets Perl regex.

grep -o pattern file.txt **shows only the matched string** 
grep -bn pattern file.txt **shows row and col**  
grep -v pattern file.txt **inversion**  
grep -A 3 pattern file.txt  **3+ lines after**	
grep -B 3 pattern file.txt **3+ lines before**  
grep -c pattern file.txt **count matches**  
grep -l/L pattern *.txt **list of files that matches/doesn't**  
grep -f patterns.txt file.txt **seach using patterns file**  
grep "stuff\|more" demo.txt **OR**  
grep -E "stuff|more" demo.txt **OR**  
grep -w "of" table.txt **search only for the entire word**  
grep -x "of" table.txt **search only for the entire line**  
grep -r menu /boot **recursive**  
grep '\\<kot\\>' kot.txt **word starts/end with kot**  
grep -e '--pattern' test.txt **match exact pattern**  
grep -r [-l, --binary-files=without-match]  "test" ./  **without binary files**  recursive search

More examples

grepc -E '(co)lor\1{2}' logs.txt Using backrefence to match colorcoco
cat test.txt | grep --color=always -E 'GET\s/example/\?p=1' or grep 'GET\s/example/?p=1' gives: GET /example/?p=1

echo "\ab e\f \. " | grepc -Eo '\\.' finds \a \f \.

Classes

link

Quantifiers

  • * - 0 or more
  • *? - 0 or more non greedy
  • + - 1 or more
  • +? - 0 or 1 non greedy
  • ? - 0 or 1
  • ?? - 0 or 1 non greedy
  • {m} - exactly 'm'
  • {m,n} - from m to n (m defaults to 0, n to infinity)
  • {m,n}? - from m to n, as few as possible

Character classes

\b - backspace(0x08)(inside[]only) \b - word boundary(outside[]only)
\B - non-word boundary \d - digit, same as[0-9]
\D - non-digit
\S - non-whitespace character \s - whitespace character[ \t\n\r\f]
\W - non-word character
\w - word character[0-9A-Za-z_]

Groups and lookaround

Positive lookahead matches a group after the main expression without including it in the result.
\d(?=px) 1pt 2px 3em 4px Negation lookahead specifies a group that can not match after the main expression (if it matches, the result is discarded).
\d(?!px) 1pt 2px 3em 4px
Positive lookbehind
(?<=\d)px matches 1pt 2px 3em 4px
Negative lookbehind
(?<!\d)px matches 1pt 2px 3em 4px ppt

^.*\/ grab everything before the slash. (\ is for escaping forwardslash )
"[^"]*" This will match a quoted text part without additional quotes in it
Modern RegEx Flavors (PCRE)

Special characters that must be escaped

Includes C, C++, Delphi, EditPad, Java, JavaScript, Perl, PHP (preg), PostgreSQL, PowerGREP, PowerShell, Python, REALbasic, Real Studio, Ruby, TCL, VB.Net, VBScript, wxWidgets, XML Schema, Xojo, XRegExp. PCRE compatibility may vary

Anywhere: . ^ $ * + - ? ( ) [ ] { } \ |

Legacy RegEx Flavors (BRE/ERE)

Includes awk, ed, egrep, emacs, GNUlib, grep, PHP (ereg), MySQL, Oracle, R, sed. PCRE support may be enabled in later versions or by using extensions

ERE/awk/egrep/emacs

Outside a character class: . ^ $ * + ? ( ) [ { } \ |
Inside a character class: ^ - [ ]

BRE/ed/grep/sed

Outside a character class: . ^ $ * [ \
Inside a character class: ^ - [ ]
For literals, don't escape: + ? ( ) { } | /
For standard regex behavior, escape: \+ \? \( \) \{ \} \|

More examples

(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/? url
<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>) html tag
([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6}) email
-?\d+ Integer
(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?) IP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment