Friday, November 6, 2009

REGEXP - Regular Expressions

A regular expression is a string of characters designed to search for or replace another string of characters. This looks pretty basic but the power of a regular expression should not be underestimated.

Regular expressions are used by some of the most powerful unix tools (grep, sed, awk) and some programming languages like Perl.

Learning regexp's is the step required to move from a regular basic Unix user to a power user.

The basics

If you use "golf" in your regexp you won't find the word "Golf" 

If you use "[Gg]olf" in your regexp you will find "golf and Golf"

You can also use "g[oe]lf" to find "golf or gelf"

But what if you want to find also gilf?

you can use "g.lf"
or maybe "g..f" 

Are we clear so far?

Searching files with grep

Now let's go a little bit further

let's say you have a file called myfile.txt with the following text...

I like golf.
Golf is played on grass.
I created gilf.

The basic sintax for the grep command is:

grep regexp filename

If you type:
    grep golf mytext.txt
you will get:
    I like golf.

If you type:
    grep [gG]olf mytext.txt
you will get:
    I like golf.
    Golf is played on grass.

Etc. etc. etc.... You got the idea right?

Take a look at the following table ...


And this is all for today!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.