Chapter 3. Regular Expressions

Table of Contents

What are regular expressions?
The fullstop
Let's explore "sed" syntax
Square brackets ( [ ] ), the caret ( ^ ) and the dollar ( $ )
Using sed and pipes
The splat (asterisk) ( * )
The plus operator ( + )
Returning from detour to our discussion on curly braces …
RE's are greedy for matching patterns
Placeholders and word boundaries
Word boundaries ( < and > ) - a formal explanation
The tr command
The cut command
First Example in stages:
Second Example in stages:
Third example in stages
The paste command
The uniq command
The Sort command
The grep command
grep, egrep and fgrep
Challenge sequence:


One of the most important (and powerful) parts of UNIX and Linux, and one that is often overlooked, is the concept of regular expressions.

Regular expressions could change your life (in the Linux world!!!) and if you don't know how to use them you will find yourself handicapped.

This chapter will not only teach you about regular expressions it will also teach how and when to use them.

  1. First log in using your username.

  2. Then you need files containing data. Go to the site and retrieve the file bazaar.txt. This is an extract from Eric S Raymond's book, "The Cathedral and the Bazaar" (the team highly recommends this book as per the preface to this course). All files you download from the site should be placed in your user area. Use the 'less' command to browse the file to familiarise yourself with it's contents. Repeat the process for the files:

  3. Thirdly, we need a program within Linux that uses regular expressions. For this we're going to use a program called sed. sed is one of those many "Swiss army knives" that you're going to find in Linux.

[Note] Note

Some of the other tools that you will use regularly are: find, awk, perl and grep.