--nextPart7349955.xA6aG7j4qs
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
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 =3D 16
my_file_contents =3D File.open('foo'){|f| f.read }
my_file_contents =3D~ /find this string(.{#{READ_BYTES}})/
my_data =3D $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!
=2D-=20
Konrad Meyer <
[email protected]>
http://konrad.sobertillnoon.com/
--nextPart7349955.xA6aG7j4qs
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
iD8DBQBGvCtkCHB0oCiR2cwRAv7VAJ9TJuiLAgr0K4ffsz29WyXwnbnOPwCdHALl
7DH6vHJ6ZAfPS0YtiQ9Etsc=
=PfL0
-----END PGP SIGNATURE-----
--nextPart7349955.xA6aG7j4qs--