Patten match

Hi Good morning,

      I am having one text file. In this I need to remove all the new lines
and I need to match the keyword what I am giving.

Please any one help me....

Thanks,
S.Sangeetha.

Please don't be a "vampire": slash7 with Amy Hoy » Blog Archive » Help Vampires: A Spotter’s Guide

Start with the official Ruby website, try to learn, then come back if
you have questions.

Jeff
softiesonrails.com

···

On Jun 19, 12:24 am, geetha <sangeetha.geeth...@gmail.com> wrote:

Hi Good morning,

      I am having one text file. In this I need to remove all the new lines
and I need to match the keyword what I am giving.

Please any one help me....

For removing newlines, you can use the String#gsub method, like this:

"one line\nother line".gsub("\n",'')

don't forget to use double quotes on the "\n" - they tell Ruby to
interpret the "slash-letter" combos as special characters, rather than
as a slash followed by a letter. "\n" is a newline.

For matching a word, you can use regular expressions - they're a
fairly interesting, but somewhat complicated topic, and you would be
much better served by looking for a Ruby regular expression tutorial
in Google.

···

On 6/19/07, geetha <sangeetha.geethu05@gmail.com> wrote:

Hi Good morning,

      I am having one text file. In this I need to remove all the new lines
and I need to match the keyword what I am giving.

Please any one help me....

Thanks,
S.Sangeetha.

--
Bira

http://sinfoniaferida.blogspot.com

Hmm removing the newlines can be done in different ways, I'll show you
one below.
However it is not clear to me what you mean by matching keywords, as a
sample application I will replace the keyword by _keyword_, but maybe
you shall ask again.

def remove_nl_and_kw file_name, key_word="ruby"
   File.readlines(file_name).
     map{|line| line.chomp}.
     gsub(key_word, "_" << key_word << "_").
     join

end

If you had the newlines removed because keywords might spwan lines you
have to put join in front of gsub, but that might become a performance
nightmare for larger files as you will be calling gsub on *huge*
strings.

HTH
Robert

···

On 6/19/07, geetha <sangeetha.geethu05@gmail.com> wrote:

Hi Good morning,

      I am having one text file. In this I need to remove all the new lines
and I need to match the keyword what I am giving.

Please any one help me....

Thanks,
S.Sangeetha.

--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

> Please any one help me....

cries someone

Please don't be a "vampire": slash7 with Amy Hoy » Blog Archive » Help Vampires: A Spotter’s Guide

That whole article suggests -- albeit unintentionally -- a nice
comfortable ride towards elitism

Is "eitism" a word? I think I made it up. I didn't have the patience
to look it up at www.m-w.com. Just like I didn't have the patience to
read the entire ruby source code to answer my own questions so that I
won't waste anyone's time here :slight_smile:

···

On 6/19/07, Jeff <cohen.jeff@gmail.com> wrote:

On Jun 19, 12:24 am, geetha <sangeetha.geeth...@gmail.com> wrote:

Start with the official Ruby website, try to learn, then come back if
you have questions.

Jeff
softiesonrails.com

Robert Dober wrote:

def remove_nl_and_kw file_name, key_word="ruby"
   File.readlines(file_name).
     map{|line| line.chomp}.
     gsub(key_word, "_" << key_word << "_").
     join

end

If you had the newlines removed because keywords might spwan lines you
have to put join in front of gsub, but that might become a performance
nightmare for larger files as you will be calling gsub on *huge*
strings.

Robert,

Nice tight illustrative code, but it needs a newbie correction. If join
is to be at the end, then you need

def remove_nl_and_kw( file_name, key_word="ruby")
   File.readlines(file_name).
     map{|line| line.chomp.gsub(key_word, "_" << key_word << "_")}.
     join
end

Now I have another newbie question. When is the file closed?
end of readlines?
end of statement?
end of function call?
end of program?

Thanks for patience.
Ian

···

--
Posted via http://www.ruby-forum.com/\.

Robert Dober wrote:

> def remove_nl_and_kw file_name, key_word="ruby"
> File.readlines(file_name).
> map{|line| line.chomp}.
> gsub(key_word, "_" << key_word << "_").
> join
>
> end
>
> If you had the newlines removed because keywords might spwan lines you
> have to put join in front of gsub, but that might become a performance
> nightmare for larger files as you will be calling gsub on *huge*
> strings.

Robert,

Nice tight illustrative code, but it needs a newbie correction. If join
is to be at the end, then you need

Well spotted, thx for correcting it :), will teach me to copy lines around.

def remove_nl_and_kw( file_name, key_word="ruby")
   File.readlines(file_name).
     map{|line| line.chomp.gsub(key_word, "_" << key_word << "_")}.

that's it very nice

     join
end

Now I have another newbie question. When is the file closed?
end of readlines?

that's it, as a matter of fact this is one of my favorite properties
of the class methods
#IO.read, #IO.readlines and #IO.open with a block.
of program?

Thanks for patience.
Ian

Thank *you*
Robert

···

On 6/30/07, Ian Whitlock <iw1junk@comcast.net> wrote:

--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck