# Patten match

**URL:** <https://www.ruby-forum.com/t/patten-match/107209>\
**Category:** Ruby\
**Created:** [June 19, 2007, 7:25am UTC](https://www.ruby-forum.com/t/patten-match/107209 "2007-06-19T07:25:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![geetha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@geetha](https://www.ruby-forum.com/u/geetha)\
**Post date:** [June 19, 2007, 7:25am UTC](https://www.ruby-forum.com/t/patten-match/107209/1 "2007-06-19T07:25:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![geetha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@geetha](https://www.ruby-forum.com/u/geetha)\
**Post date:** [June 19, 2007, 8:11am UTC](https://www.ruby-forum.com/t/patten-match/107209/2 "2007-06-19T08:11:37Z")

</div>

On Jun 19, 12:24 am, geetha [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](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

> **[Hey Daddio!](http://softiesonrails.com)**
>
> Home of Hey Daddio Software

---

<div class="post-metadata">

**Author:** ![geetha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@geetha](https://www.ruby-forum.com/u/geetha)\
**Post date:** [June 29, 2007, 10:47pm UTC](https://www.ruby-forum.com/t/patten-match/107209/3 "2007-06-29T22:47:23Z")

</div>

On 6/19/07, geetha [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![geetha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@geetha](https://www.ruby-forum.com/u/geetha)\
**Post date:** [June 20, 2007, 6:24pm UTC](https://www.ruby-forum.com/t/patten-match/107209/4 "2007-06-20T18:24:09Z")

</div>

On 6/19/07, Jeff [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Jun 19, 12:24 am, geetha [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://slash7.com/articles/2006/12/22/vampires)

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](http://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 🙂

---

<div class="post-metadata">

**Author:** ![geetha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@geetha](https://www.ruby-forum.com/u/geetha)\
**Post date:** [June 30, 2007, 1:37pm UTC](https://www.ruby-forum.com/t/patten-match/107209/5 "2007-06-30T13:37:19Z")

</div>

On 6/19/07, geetha [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![geetha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@geetha](https://www.ruby-forum.com/u/geetha)\
**Post date:** [June 30, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/patten-match/107209/6 "2007-06-30T15:45:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![geetha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f08c70/32.png) [@geetha](https://www.ruby-forum.com/u/geetha)\
**Post date:** [June 30, 2007, 4:38pm UTC](https://www.ruby-forum.com/t/patten-match/107209/7 "2007-06-30T16:38:33Z")

</div>

On 6/30/07, Ian W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
