Ext2: Scripting: Regular expressions

[ Thanks to Rob
for this link. ]

“Imagine, if you will, that you are reading a new novel, and
almost finished with it, but the author makes a reference to a
comment someone made somewhere in the first hundred pages;
unfortunately, you can’t remember exactly what it was, who said it,
or, worse, just where in the book it was said. Since you’re using a
paper book, you have very few options, other than trying to page
through the book and hope to catch what you’re looking for flip
by… assuming your recollections of what was said are

“However, if instead of a book, you were looking for a vaguely
remembered pattern of text in a very large text file, you would not
be quite so out of luck. Traditionally, Unix has provided a very
nice group of utilities for performing different operations on
text. In this case, the first one you would want to use would
probably be grep, to look for the text in the entire file. However,
you have a problem — you don’t remember how, exactly, what you’re
looking for was formatted, so you can’t search for it. What to

“This is where regular expressions come in. A regular
expression (also known as a regex or regexp) is a formal method for
specifying a pattern of text.
The simplest pattern is, of
course, the exact word or phrase you want to match; the simplest
way to match ‘cat’ would be with the regex ‘cat’. In this case,
each letter in the regex ‘cat’ stands for a letter in the text
you’re looking for that it must match exactly. If you execute the
command grep ‘cat’ file, grep will look through the file ‘file’,
and report any occurrences of the word ‘cat’.”


Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends, & analysis