Reading file text

I have a file which I want to extract specific lines and put them together
into a new string, for an example this is what the input file is

How about this ?

ruby a.rb a.rb
cat results.txt
file = ARGV[0] has the text result = File.open(“results.txt”, “w”) right below it
lines = IO.readlines(file) has the text while lines.size > 1 right below it
a, b = lines.slice!(0, 2) has the text a.chop! right below it
b.chop! has the text result.puts “#{a} has the text #{b} right below it” right below it
end has the text result.close right below it
cat a.rb
file = ARGV[0]
result = File.open(“results.txt”, “w”)
lines = IO.readlines(file)
while lines.size > 1
a, b = lines.slice!(0, 2)
a.chop!
b.chop!
result.puts “#{a} has the text #{b} right below it”
end
result.close

···

On Tue, 24 Jun 2003 13:30:06 +0000, Dan wrote:


Simon Strandgaard

“Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com schrieb im
Newsbeitrag news:pan.2003.06.24.17.34.58.162942@sneakemail.com

I have a file which I want to extract specific lines and put them
together
into a new string, for an example this is what the input file is

How about this ?

It has the major disadvantage that it blows for large files because it has
to read them in completely.

If you know that lines always alternate you can do

def scan1(io)
label = nil

io.each do |line|
line.chomp!

case io.lineno % 2
  when 1 # label
    label = line
  when 0 # text
    puts "[#{label}] has the text [#{line}] right below it"
end

end
end

File.open(“sc.txt”, “r”) do |io|
puts “scan1”
scan1 io
end

if you know the pattern you can do

def scan2(io)
label = nil

io.each do |line|
line.chomp!

case line
  when /^\s*line\s+\d+\s*$/o # label
    label = line
  else # text
    puts "[#{label}] has the text [#{line}] right below it"
end

end
end

File.open(“sc.txt”, “r”) do |io|
puts “scan2”
scan2 io
end

robert
···

On Tue, 24 Jun 2003 13:30:06 +0000, Dan wrote: