# Is there a easy way to find and replay a match

**URL:** <https://www.ruby-forum.com/t/is-there-a-easy-way-to-find-and-replay-a-match/138920>\
**Category:** Ruby\
**Created:** [May 29, 2008, 12:07am UTC](https://www.ruby-forum.com/t/is-there-a-easy-way-to-find-and-replay-a-match/138920 "2008-05-29T00:07:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 29, 2008, 12:07am UTC](https://www.ruby-forum.com/t/is-there-a-easy-way-to-find-and-replay-a-match/138920/1 "2008-05-29T00:07:42Z")

</div>

Hi,there

If I have file and has a line is “aaa=100”, and need to replace it as  
“aaa=4”.  
Is there a way to do it without iterating each line and find the match,  
then replace it? Thank in advance

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 29, 2008, 12:20am UTC](https://www.ruby-forum.com/t/is-there-a-easy-way-to-find-and-replay-a-match/138920/2 "2008-05-29T00:20:47Z")

</div>

> If I have file and has a line is “aaa=100”, and need to replace it as  
> “aaa=4”.

> puts File.read(‘somefile’)  
> foo  
> aaa=100  
> bar

> puts File.read(‘somefile’).sub(‘aaa=100’, ‘aaa=4’)  
> foo  
> aaa=4  
> bar

Tor Erik

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 29, 2008, 1:13am UTC](https://www.ruby-forum.com/t/is-there-a-easy-way-to-find-and-replay-a-match/138920/3 "2008-05-29T01:13:22Z")

</div>

I need “aaa=4” in the file instead of just print out

Tor Erik L. wrote:

> > If I have file and has a line is “aaa=100”, and need to replace it as  
> > “aaa=4”.
> 
> > puts File.read(‘somefile’)  
> > foo  
> > aaa=100  
> > bar
> 
> > puts File.read(‘somefile’).sub(‘aaa=100’, ‘aaa=4’)  
> > foo  
> > aaa=4  
> > bar
> 
> Tor Erik

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 29, 2008, 3:01am UTC](https://www.ruby-forum.com/t/is-there-a-easy-way-to-find-and-replay-a-match/138920/4 "2008-05-29T03:01:52Z")

</div>

On Wednesday 28 May 2008 18:18:09 Cheyne Li wrote:

> I need “aaa=4” in the file instead of just print out

No offense, but this was pretty much handed to you. You should be able  
to get  
the rest of the way yourself…

> > > print File.read(‘somefile’).sub(‘aaa=100’, ‘aaa=4’)

string = File.read(‘somefile’).sub(‘aaa=100’,‘aaa=4’)  
open(‘somefile’,‘w’){|f| f.write string}

By the way: DON’T do it this way in a real program. It’s not at all  
safe.  
Learn about temporary files and file IO.

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 29, 2008, 3:05am UTC](https://www.ruby-forum.com/t/is-there-a-easy-way-to-find-and-replay-a-match/138920/5 "2008-05-29T03:05:37Z")

</div>

Cheyne Li wrote:

> I need “aaa=4” in the file instead of just print out

File.open(‘somefile’, ‘r+’) do |file|  
content = file.read.sub(‘aaa=100’, ‘aaa=4’)  
file.seek(0)  
file.write(content)  
}

Read more about File at  
[http://www.ruby-doc.org/core/classes/File.html](http://www.ruby-doc.org/core/classes/File.html)

although this documentation isn’t the easiest to follow.

Tor Erik

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 29, 2008, 4:19am UTC](https://www.ruby-forum.com/t/is-there-a-easy-way-to-find-and-replay-a-match/138920/6 "2008-05-29T04:19:08Z")

</div>

Hi,

Cheyne Li wrote:

> Hi,there
> 
> If I have file and has a line is “aaa=100”, and need to replace it as  
> “aaa=4”.  
> Is there a way to do it without iterating each line and find the match,  
> then replace it? Thank in advance

ruby -i -pe’gsub(/aaa=100/,“aaa=4”)’ somefile

Regards,

Park H.

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 29, 2008, 11:44pm UTC](https://www.ruby-forum.com/t/is-there-a-easy-way-to-find-and-replay-a-match/138920/7 "2008-05-29T23:44:23Z")

</div>

Thanks a lot, it really helped. But I don’t know why if i use regular  
expression to match the pattern “aaa=.\*”, it’s not working with sub, but  
gsub.

Tor Erik L. wrote:

> Cheyne Li wrote:
> 
> > I need “aaa=4” in the file instead of just print out
> 
> File.open(‘somefile’, ‘r+’) do |file|  
> content = file.read.sub(‘aaa=100’, ‘aaa=4’)  
> file.seek(0)  
> file.write(content)  
> }
> 
> Read more about File at  
> [class File - RDoc Documentation](http://www.ruby-doc.org/core/classes/File.html)
> 
> although this documentation isn’t the easiest to follow.
> 
> Tor Erik
