On Jul 11, 2007, at 4:00 AM, John Joyce wrote: > > On Jul 11, 2007, at 3:35 AM, Marc Hoeppner wrote: > >> >> Hi everyone, >> >> absolute beginner here, so bare with me ;) >> >> The following issue came up and I can't figure out how to solve. >> Probably nothing very complicated, tho. >> >> I want to load a text file via ARGV[0] and then search it for a key >> word. If this key word is found, it should be printed (puts) >> and...and >> this is very I am stuck ...it should also included the next 8 >> lines. The >> file structure looks like this: >> >> KOG0003 >> Sc 000000000 >> Dr 000000000 >> Ar 001010011 >> Ca 000100100 >> Ho 001010010 >> Sa 010000000 >> An 100000100 >> Pl 000001000 >> KOG0009 >> Sc 000100 >> Dr 100000 >> Ar 001001 >> Ca 101000 >> Ho 101010 >> Sa 010000 >> An 100000 >> Pl 001000 >> >> So lets say I want to select for a specific KOG (or a list of >> KOGs) and >> write them together with the associated lines into a new file. I >> guess >> the issue at the moment is, that when I open the ARGV[0], it reads >> one >> line at a time - cant quite figure out how to include the "go to next >> line and puts it, too" part. >> >> Any help pointing me in the right direction would be greatly >> appreciated >> >> /Marc >> >> -- >> Posted via http://www.ruby-forum.com/. >> > > You can use a heredoc to maintain all the whitespace. > Here's some info: > http://blog.jayfields.com/2006/12/ruby-multiline-strings-here-doc- > or.html > Then just assign the heredoc to a variable > last_8_lines = <<-heredoc_ender > KOG0003 > Sc 000000000 > Dr 000000000 > Ar 001010011 > Ca 000100100 > Ho 001010010 > Sa 010000000 > An 100000100 > Pl 000001000 > KOG0009 > Sc 000100 > Dr 100000 > Ar 001001 > Ca 101000 > Ho 101010 > Sa 010000 > An 100000 > Pl 001000 > heredoc_ender > > When you want to use it, just : > puts last_8_lines > > You can also use string interpolation with heredocs > > > #{keyword_string} > > John Joyce > Oops, I misunderstood the OP! You might also use ri to look up "readlines" your loop mechanism could be done many ways. but Robert gave one way. Definitely read one line at a time, searching for the string. if found, start counting. 8 lines assign the search line to a variable, keep reusing that variable until you get a match, after the match, the next 8 lines are each just concatenated onto the end of the variable.