[Tutor] find and replace relative to an nearby search string in afile
Alan Gauld
alan.gauld at btinternet.com
Mon Jul 13 12:17:31 CEST 2009
"Pete O'Connell" <pedrooconnell at gmail.com> wrote
> I am always looking for the line " name Write1" as my starting point. In
> the
> first example below, I want to replace the path, which is 2 lines above
> it.
> I have made a basic script to do that and it works fine. The problem I am
> having is when the number of lines between " name Write1" and the path
> above
> it is not 1, my script breaks.
Why not turn the algorithm around and look for the pasth line,
then see if a name Write1 line exists in the same block.
for each Write block:
find path line and store it
find name Write1
if exists: update path
That way you have no dependency on the number of lines and the
only tricky bit is if the path comes after the name Write1 line
Or if there is no path line....
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list