Regular Expressions

Regular Expressions

Most of the search mechanisms listed in this chapter use regular expressions in one form or another. A regular expression is a mathematical mechanism for specifying the ordering of symbols. Historically, regular expressions originated from discussions of the Theory of Computation, a seriously hard-core branch of mathematics that is far removed from the day-to-day rigors of, say, locating the smutty email to your girlfriend that you misplaced in some gargantuan file system at work.

However, the same principles govern both tasks. To get full value for your Linux dollar, you will need some understanding of regular expressions and how they are used to specify search patterns.

Regular expressions exist to give you a mechanism to specify patterns of characters. The implementation of a regular expression includes three classes of characters:

literals

The literal character you typed in (a, b, c , 1, 2, 3 etc.).

wildcards

Special characters used to represent one or more characters other than themselves. For example, the "*" character will match any number of any other characters ("d*" matches "date", "day", "dally", and anything else starting with the letter d.) The "." character will match one instance of any other character ("d.te" matches with "date" and "dote").

metacharacters

Metacharacters are characters that have a special meaning. For example, the caret character "^" usually matches the beginning of a line. The "$" character matches the end of a line.

In addition, it is possible to specify groups of characters. For example, the regular expression "[aAbB]*"would match any string of any length that started with the letter a or b, either uppercase or lowercase. (This group is delimited by square braces.) See the grep entry for more information about the implementation of regular expressions.

The commands covered in this section include

egrep

Grep with extended regular expressions

find

Search the directory tree

finger

Display information about a user

fgrep

Grep variation for matching fixed strings

grep

Search for a pattern in a file

locate

Search the locate database for a file

updatedb

Update the locate database

which

Search the directories of your $PATH for a file

 



Linux Desk Reference
Linux Desk Reference (2nd Edition)
ISBN: 0130619892
EAN: 2147483647
Year: 2000
Pages: 174
Authors: Scott Hawkins

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net