From: Brian Candler Date: 2008-10-28T06:16:32+09:00 Subject: Re: file manipulation Vandana wrote: > Suppose I have the following structure in a file : > --------------------- > Instance J1 ( > net n1() > net n2 () > net n3() > ) > Instance J2 ( > net n1() > net n2() > net n3() > ) > Instance J3 ( > net n1() > net n2() > net n3() > ) > ----------------------- > As an example, I want to read J3/net n3. > (the files are huge ....in GB) > I can grep for n3 but it will return 3 instances of n3. > > How can I ensure that Im reading n3 values that belong to instance > J3? Using (Unix shell command) grep, or using Ruby? In Ruby you could just set a variable whenever you see a line matching /Instance \S+/, so when you see a line matching /n3/ you can check what the preceding Instance was. Reading multiple gigabytes this way is never going to be efficient, unless you have enough GB to keep the whole dataset in RAM. If not, then consider indexing the data, perhaps with something like cdb. This would let you jump immediately to the data for instance J3 without scanning through the whole file. -- Posted via http://www.ruby-forum.com/.