Using Grep & Regular Expressions

Table of Content
  1. 1. Basic Usage
  2. 2. A Little bit about Regular Expressions
    1. 2.1. Anchor Matches
    2. 2.2. Matching Any Character
    3. 2.3. Bracket Expressions
    4. 2.4. Repeat Pattern Zero or More Times
    5. 2.5. Escaping Meta-Characters
  3. 3. Extended Regular Expressions
    1. 3.1. Groupinn
    2. 3.2. Alternation
    3. 3.3. Quantifiers
    4. 3.4. Specifying Match Repetition

From Using Grep & Regular Expressions to Search for Text Patterns in Linux

Basic Usage

1
$ grep [Pattern] [Input File]

Print out every line in the file containing that Pattern

Common Options Description
-i == --ignore-case both upper- and lower-case variations
-v == --invert-match do not contain the Pattern
-n == --line-number show the line number

A Little bit about Regular Expressions

Anchor Matches

Anchor Matches Description
^ at the begining of the string(line)
$ at the end of the string

Matching Any Character

Any Character Description
. match any single character

Bracket Expressions

  • Characters between [ and ] means OR
  • But if the characters start with ^, it means NOT, i.e. except
  • Represent a range of characters: [A-Z], but we can use POSIX character classes to replace the [A-Z]: [[:upper:]]

Repeat Pattern Zero or More Times

Character Description
* repeat the previous character or expression zero or more times

Escaping Meta-Characters

Using backslash character \ to escape characters that would normally have a special meaning.

Extended Regular Expressions

Use -E flag or call egrep command to use Extended Regular Expressions

Groupinn

Alternation

Quantifiers

Character Description
? match the previouscharacter zero or one times
+ one or more times

Specifying Match Repetition

{NUM} to specify matching times, from an exact number, a range, or an upper or lower bounds