Table of Contents

Grep - Regex Usage

A regular expression is a compact way of describing complex patterns in text.

With grep, you can use them to search for patterns


Assuming a file exists with the following contents:

boot
book
booze
machine
boots
bungie
bark
aardvark
broken$tuff
robots

Search the file for lines ending with the letter e.

grep "e$" filename

returns:

booze
machine
bungie

grep -E "boots?" filename

returns:

boot
boots

NOTE: grep -E is also known as egrep.

The regexp command ? will match 1 or 0 occurrences of the previous character.


Combine Multiple Searches

grep -E "boot|boots" filename

returns:

boot
boots

NOTE: The pipe | means 'or'.


Special Characters

grep '\$' filename

returns:

broken$tuff

NOTE:

The **-F– option can also be used, which stands for 'fixed string' or 'fast' in that it only searches for literal strings and not regexps.