On 7/11/07, Marc Hoeppner <marc.hoeppner / molbio.su.se> 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 read all files indicated by args or stdin with ARGF
ARGF.readlines will slurp in the whole file, but if it is large that
might be a problem.

Let us try a robust solution first

count = 0
ARGF.each do |line|   ## treating a line at turn
    puts line unless count.zero?
    count = [0, count-1].max
    if line =~ /your keyword or something/ then
       puts line
       count = 8
   end
end

Now that is one ruby solution, maybe you want to use grep, sorry if
this is noise but maybe this helps too.
  grep -A8 youre_keywoarde_blease ...

HTH
Robert


>


-- 
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck