[Ilugc] Perl regular expression

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Wed Sep 26 04:54:07 2007

On Tue, Sep 25, 2007 at 05:02:24PM +0530, Ravi Jaya wrote:

 Dear luggies,

 I want to grep from the  file, the words which start with  & and close
with  ; eg(   ,& ...) to write this is in to another file.  how
could i  fetch an individual word. from the file.  It is regx is an ideal
way for using this

Hundreds in lug can help you with this but I will be the one to bell the
cat.

$ grep '&.*;' foo.txt 

seems to get you the relevant lines.

Now if you are interested in merely catching hold of the words like
& then you will have to use the capture feature of regexes. Perl and
sed normally use '(' and ')' around the regex for this.

So you would do something like this.

Say you have a file foo.txt with the following

<foo.txt>

What is going on?

This line messed is up.

</foo.txt>

Now if you want to swap the words 'is' and 'messed' you can do this.

$ perl -pi -e 's/(messed).*(is)/\2 \1/' foo.txt

Here '\1' captures the first string within '(' ')' and '\2' captures
the second.

So you can play with them any way you like. But remember that regexes
work only for the match section, not for the substitute section.

Hopefully this will get you started with regexes.

In your case you will have an expression like this.
 
-e 's/(&.*;)/'


 please help me with the examples or the resources where i could dig more
abt
 regx.

Check out this page.

http://gnosis.cx/publish/programming/regular_expressions.html

Best of luck!

Enjoy regexes!

I usually have it for breakfast since it can give you sleepless nights
if you take it for dinner. :)

regards,
Girish

Other related posts: