From: David Vallner Date: 2006-12-04T03:41:11+09:00 Subject: Re: Can one use line addresses...to select a portion of file --------------enig347893002C41682FD3EDC14D Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Jacob, Raymond A Jr wrote: > I would like to select a portion of a file to process like I did with > sed. >=20 > i.e. a file called Menu with the following contents: > __McDonalds__ >=20 > ... > ... > ...(stuff deleted) >=20 > _Happy_Meal__ >=20 > With sed I would do something like >=20 > sed -n "/__McDonalds__/,/__Happy_Meal_/p" Menu >=20 > I can not find anything comparable in Ruby. I may be using the wrong > words when I searched google for ruby line address. > Any help will be appreciated. >=20 > Thank you, > Raymond >=20 For a trivial approach, and the most terse one: lines =3D file.to_a lines_to_process =3D lines[(lines.index("__McDonalds__\n")) .. lines.index("__Happy_Meal__\n"))] Requires loading the whole file into memory, can only, and several passes through it, so the performance is probably ugly, can only handle lines, and can't cope with regular expressions (which is why you need the \n in the delimiters). But probably Good Enough for some applications= =2E A more involved solution would probably be one yielding lines between lines that match the delimiters to a block or returning an array of them if no block was given. The most flexible one would be "faking" an IO object that would start at the starting pattern and then look for the ending one as it goes, but unless you need the data without line breaks and without the memory overhead of keeping them in memory, probably too much bother. David Vallner --------------enig347893002C41682FD3EDC14D Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (MingW32) iD8DBQFFcxpDy6MhrS8astoRApdIAJ95Rm8G4cNQzrnxmIfIdhxzATBWbgCfTorp TKNUvcz4CJrBNPHDQRe/lxg= =lM3v -----END PGP SIGNATURE----- --------------enig347893002C41682FD3EDC14D--