grep a block

B

beny 18241

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
 
B

beny 18241

ok closed Ifigue it out :)

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

beny18241

beny said:
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
 
R

Robert Klemme

2009/12/18 beny 18241 said:
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
 
W

William James

beny said:
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
----

The tiny language Awk is good for things like this.

awk "/<mms:MmsCcppAccept>/,/<\/mms:MmsCcppAccept>/" file

--
 
B

Bertram Scharpf

Hi,

Am Freitag, 18. Dez 2009, 23:39:40 +0900 schrieb Robert Klemme:
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:

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Using popen & grep 2
grep a block of code 2
Grep via Ruby 10
eval within grep not working 1
Block variables 2
Re-naming a block 2
send() with a block? 22
Passing a named function instead of a code block? 12

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top