Friday, January 22, 2010

Begining Regular Expressions -Part1

A regular expression, usually abbreviated to "regex" or "regexp", describes text patterns. In unix world there are several tools which utilizes the advantage of using in processing text files.
We will begin by using 'grep',which is a pattern matching tool in unlix/linux for regexp operations. We are using POSIX dialect which is different from perl dialect.

Some terms:
Anchors : Anchors are used to specify the position of the pattern in relation to a line of text.[eg:^,$]
Modifiers:Modifiers specify how many times the previous character set is repeated[eg:.,*,#].
------------------------------------------------------------
^A "A" at the beginning of a line
A$ "A" at the end of a line
A^ "A^" anywhere on a line
$A "$A" anywhere on a line
^^ "^" at the beginning of a line
$$ "$" at the end of a line
-------------------------------------------------------------

Some sample outputs are given below:
['#' indicates the executed command and output is shown below]Example file: animals.txt
#cat animals.txt
elephant
tiger
bear
tigress
panda
panther
black panther

# grep panther animals.txt
panther
black panther

# grep -v panther animals.txt
elephant
tiger
bear
tigress
panda

# grep '^e' animals.txt
elephant

#grep 'a$' animals.txt
panda

No comments:

Post a Comment