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.

On Jun 19, 12:24 am, geetha [email protected] 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…

Please don’t be a “vampire”:
http://slash7.com/articles/2006/12/22/vampires

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

Jeff

On 6/19/07, geetha [email protected] 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.

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, Jeff [email protected] wrote:

On Jun 19, 12:24 am, geetha [email protected] wrote:

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, geetha [email protected] 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.

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

Robert D. 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

On 6/30/07, Ian W. [email protected] wrote:

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