[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

newbie question: how to seek in a file

Ak 756

8/10/2007 8:43:00 AM

Hi

I want to find a string in a text file, then read some bytes data after
the position of this string. Would anybody kindly help to tell me how to
do?

Thanks in advance.
--
Posted via http://www.ruby-....

7 Answers

Lutz Horn

8/10/2007 9:07:00 AM

0

Hi,

Ak 756 wrote:
> I want to find a string in a text file, then read some bytes data after
> the position of this string. Would anybody kindly help to tell me how to
> do?

You could do something like this:

File.open("my-file.txt") do |f|
i = f.read.index(/search/)
f.seek(i)
puts f.read(10)
end

Lutz
--
Posted via http://www.ruby-....

Konrad Meyer

8/10/2007 9:10:00 AM

0

On Friday 10 August 2007 01:43:13 am Ak 756 wrote:
> I want to find a string in a text file, then read some bytes data after
> the position of this string. Would anybody kindly help to tell me how to
> do?

If this is what you really want, you can do something like:

READ_BYTES = 16
my_file_contents = File.open('foo'){|f| f.read }
my_file_contents =~ /find this string(.{#{READ_BYTES}})/
my_data = $1

Of course, this is no good for large files. Generally if you're trying to
extract data from a large text file though, you should already know where it
is (constant width records) or you have to parse through everything before
the record you want to get at the one you do (csv, xml?).

If this is for a configuration file or some sort of semi-static storage, I'd
recommend using YAML or Marshal instead of making your own parser.

Cheers!
--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertil...

Konrad Meyer

8/10/2007 9:14:00 AM

0

On Friday 10 August 2007 02:06:53 am Lutz Horn wrote:
> Hi,
>
> Ak 756 wrote:
> > I want to find a string in a text file, then read some bytes data after
> > the position of this string. Would anybody kindly help to tell me how to
> > do?
>
> You could do something like this:
>
> File.open("my-file.txt") do |f|
> i = f.read.index(/search/)
> f.seek(i)
> puts f.read(10)
> end

At that point though, you've already read the entire contents of the file
into memory (f.read()) so there's no point going back and seeking in the
file when:
* Seeking from memory should always be faster than from disk
* You're already ignoring the memory issues for large files
--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertil...

Konrad Meyer

8/10/2007 9:20:00 AM

0

On Friday 10 August 2007 02:09:56 am Konrad Meyer wrote:
> On Friday 10 August 2007 01:43:13 am Ak 756 wrote:
> > I want to find a string in a text file, then read some bytes data after
> > the position of this string. Would anybody kindly help to tell me how to
> > do?
>
> If this is what you really want, you can do something like:
>
> READ_BYTES = 16
> my_file_contents = File.open('foo'){|f| f.read }
> my_file_contents =~ /find this string(.{#{READ_BYTES}})/
> my_data = $1

Better yet:
READ_BYTES = 16
content = File.open('foo'){|f| f.read }
my_data = content[content.index("find this string"), READ_BYTES]
# I tried to combine Lutz's and my earlier ideas

Though this of course assumes the string is found.

> Of course, this is no good for large files. Generally if you're trying to
> extract data from a large text file though, you should already know where it
> is (constant width records) or you have to parse through everything before
> the record you want to get at the one you do (csv, xml?).
>
> If this is for a configuration file or some sort of semi-static storage, I'd
> recommend using YAML or Marshal instead of making your own parser.

--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertil...

Ak 756

8/10/2007 9:47:00 AM

0

Konrad Meyer wrote:
> On Friday 10 August 2007 02:09:56 am Konrad Meyer wrote:
>> my_data = $1
> Better yet:
> READ_BYTES = 16
> content = File.open('foo'){|f| f.read }
> my_data = content[content.index("find this string"), READ_BYTES]
> # I tried to combine Lutz's and my earlier ideas
>
> Though this of course assumes the string is found.

I don't care about huge file at present and this method works for me.
Thanks Konrad and Lutz.
--
Posted via http://www.ruby-....

Alex Young

8/10/2007 10:01:00 AM

0

Ak 756 wrote:
> Konrad Meyer wrote:
>> On Friday 10 August 2007 02:09:56 am Konrad Meyer wrote:
>>> my_data = $1
>> Better yet:
>> READ_BYTES = 16
>> content = File.open('foo'){|f| f.read }
>> my_data = content[content.index("find this string"), READ_BYTES]
>> # I tried to combine Lutz's and my earlier ideas
>>
>> Though this of course assumes the string is found.
>
> I don't care about huge file at present and this method works for me.
> Thanks Konrad and Lutz.
I believe you can also do this with RExpect, but I'm on Windows right
now and can't test it out.

--
Alex

Robert Klemme

8/10/2007 11:07:00 AM

0

2007/8/10, Ak 756 <macro.peng@gmail.com>:
> Konrad Meyer wrote:
> > On Friday 10 August 2007 02:09:56 am Konrad Meyer wrote:
> >> my_data = $1
> > Better yet:
> > READ_BYTES = 16
> > content = File.open('foo'){|f| f.read }
> > my_data = content[content.index("find this string"), READ_BYTES]

But be careful because #index returns the starting position of the
string searched for.

> > # I tried to combine Lutz's and my earlier ideas
> >
> > Though this of course assumes the string is found.
>
> I don't care about huge file at present and this method works for me.
> Thanks Konrad and Lutz.

You can even do it in one line:

bytes = File.read("foo")[/your_string(.{10})/, 1]

This reads the file into one String, does one regexp match and returns
contents of the capturing group which in this case contains arbitrary
10 characters (i.e. bytes).

Kind regards

robert