# Grep a block

**URL:** <https://www.ruby-forum.com/t/grep-a-block/180368>\
**Category:** Ruby\
**Created:** [December 18, 2009, 2:27pm UTC](https://www.ruby-forum.com/t/grep-a-block/180368 "2009-12-18T14:27:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![beny18241](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbce88/32.png) [@beny18241](https://www.ruby-forum.com/u/beny18241)\
**Post date:** [December 18, 2009, 2:27pm UTC](https://www.ruby-forum.com/t/grep-a-block/180368/1 "2009-12-18T14:27:37Z")

</div>

Hi,

Please can anybody tell how to grep a block?  
between 2 string i had few lines of code

example:  
mms:MmsCcppAccept  
1  
2  
3  
4  
5  
\</mms:MmsCcppAccept\>

i’ve tried that way but its not working, see below my code:

* * *

#!/usr/bin/ruby

filename = ARGV[0]

## File.open(filename).each do |line| if line =~ /mms:MmsCcppAccept/…/\</mms:MmsCcppAccept\>/ puts line end end

Please help me

Regards  
Beny18241

---

<div class="post-metadata">

**Author:** ![beny18241](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbce88/32.png) [@beny18241](https://www.ruby-forum.com/u/beny18241)\
**Post date:** [December 18, 2009, 2:49pm UTC](https://www.ruby-forum.com/t/grep-a-block/180368/2 "2009-12-18T14:49:11Z")

</div>

ok closed Ifigue it out 🙂

puts ARGF.read.scan(/mms:MmsCcppAccept.\*?\</mms:MmsCcppAccept\>/m)

beny18241

beny 18241 wrote:

> Hi,
> 
> Please can anybody tell how to grep a block?  
> between 2 string i had few lines of code
> 
> example:  
> mms:MmsCcppAccept  
> 1  
> 2  
> 3  
> 4  
> 5  
> \</mms:MmsCcppAccept\>
> 
> i’ve tried that way but its not working, see below my code:
> 
> * * *
> 
> #!/usr/bin/ruby
> 
> filename = ARGV[0]
> 
> ## File.open(filename).each do |line| if line =~ /mms:MmsCcppAccept/…/\</mms:MmsCcppAccept\>/ puts line end end
> 
> Please help me
> 
> Regards  
> Beny18241

---

<div class="post-metadata">

**Author:** ![beny18241](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbce88/32.png) [@beny18241](https://www.ruby-forum.com/u/beny18241)\
**Post date:** [December 18, 2009, 3:00pm UTC](https://www.ruby-forum.com/t/grep-a-block/180368/3 "2009-12-18T15:00:58Z")

</div>

try:

#!/usr/bin/ruby

filename = ARGV[0]

File.open(filename).each do |line|  
if line =~ /mms:MmsCcppAccept/…line =~ /\</mms:MmsCcppAccept\>/  
puts line  
end  
end

Is this what you’re looking for?

---

<div class="post-metadata">

**Author:** ![beny18241](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbce88/32.png) [@beny18241](https://www.ruby-forum.com/u/beny18241)\
**Post date:** [December 18, 2009, 3:41pm UTC](https://www.ruby-forum.com/t/grep-a-block/180368/4 "2009-12-18T15:41:22Z")

</div>

2009/12/18 beny 18241 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ok closed Ifigue it out 🙂
> 
> puts ARGF.read.scan(/mms:MmsCcppAccept.\*?\</mms:MmsCcppAccept\>/m)

Eric’s solution is better for large files because it requires reading  
the file line by line only. Note that it uses a special feature of  
“if” when using a range with boolean expressions: in that case  
matching state is stored and the complete expression is true when the  
first matches, stays true and only switches back to false if the  
second matches:

irb(main):001:0\> 10.times {|i| if /^3/ =~ i.to\_s … /^7/ =~ i.to\_s; puts  
i; end}  
3  
4  
5  
6  
7  
=\> 10

irb(main):002:0\> 10.times {|i| if i == 3 … i == 7; puts i; end}  
3  
4  
5  
6  
7  
=\> 10

Note: the second example is merely for demonstration purposes, it can  
also be done with a single integer range match:

irb(main):003:0\> 10.times {|i| if (3…7) === i; puts i; end}  
3  
4  
5  
6  
7  
=\> 10

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![beny18241](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbce88/32.png) [@beny18241](https://www.ruby-forum.com/u/beny18241)\
**Post date:** [December 19, 2009, 3:19pm UTC](https://www.ruby-forum.com/t/grep-a-block/180368/5 "2009-12-19T15:19:28Z")

</div>

Hi,

Am Freitag, 18. Dez 2009, 23:39:40 +0900 schrieb Robert K.:

> second matches:
> 
> 10.times {|i| if /^3/ =~ i.to\_s … /^7/ =~ i.to\_s; puts i; end}

The name of the feature is flip-flop if you like to google for it.

I’m not sure whether it really makes code more readable.

Bertram
