I can do it in sed...

Terry Hancock hancock at anansispaceworks.com
Wed Mar 16 20:06:40 EST 2005


On Wednesday 16 March 2005 06:01 pm, Kotlin Sam wrote:
> Here are the two things that I'm trying to do:
> 	In sed, I can print every line between ^start to ^end by using 
> /^start/,/^end/p. It's quick, easy, and doesn't take much time. Is there 
>   a way to do this easily in Python?

You mean you have a text file and you want to find all the lines between
a line starting with  "start" and one starting with "end".

REs are not your best method here, just do something like this:

lines = open('myfile', 'r').readlines()
printing = 0
for line in lines:
    if line[:5]=='start': printing=1
    if line[:3]=='end':  printing=0
    if printing: print line

Or something like that.  I'm sure there are cleverer ways, but
that should do what you ask for.

Cheers,
Terry

-- 
--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks  http://www.anansispaceworks.com




More information about the Python-list mailing list