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-forum.com/.
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-forum.com/.
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-forum.com/\.
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!
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?
--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertillnoon.com/
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
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
--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertillnoon.com/
> 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.
On Friday 10 August 2007 02:09:56 am Konrad Meyer wrote:
On Friday 10 August 2007 01:43:13 am Ak 756 wrote:
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.sobertillnoon.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]
# I tried to combine Lutz's and my earlier ideasThough 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-forum.com/\.
Ak 756 wrote:
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 ideasThough 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.
On Friday 10 August 2007 02:09:56 am Konrad Meyer wrote:
--
Alex
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
2007/8/10, Ak 756 <macro.peng@gmail.com>:
> On Friday 10 August 2007 02:09:56 am Konrad Meyer wrote: