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.
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.
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
On Friday 10 August 2007 02:06:53 am Lutz H. wrote:
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:
On Friday 10 August 2007 02:09:56 am Konrad M. 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]
Though this of course assumes the string is found.
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!
Ak 756 wrote:
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.
Konrad M. wrote:
On Friday 10 August 2007 02:09:56 am Konrad M. 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.
2007/8/10, Ak 756 [email protected]:
Konrad M. wrote:
On Friday 10 August 2007 02:09:56 am Konrad M. 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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs