By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,712 Members | 763 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,712 IT Pros & Developers. It's quick & easy.

I can do it in sed...

P: n/a
I have spent so much time using sed and awk that I think that way. Now,
when I have to do some Python things, I am having to break out of my
sed-ness and awk-ness, and it is causing me problems. I'm trying. Honest!

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?

Also, I frequently use something like s/^[A-Z]/~&/ to pre-pend a tilde
or some other string to the beginning of the matched string. I know how
to find the matched string, but I don't know how to change the beginning
of it while still keeping the matched part.

If I were able to stay in the *nix environment for all my work, I could
do it with these tools and the beloved pipe(|), but that isn't my lot in
life. I would do it in Perl, but, frankly, it gives me headaches even
looking at it.

Any ideas?

Thanks, Lance

Jul 18 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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

Jul 18 '05 #2

P: n/a

Kotlin Sam wrote:
I have spent so much time using sed and awk that I think that way. Now, when I have to do some Python things, I am having to break out of my
sed-ness and awk-ness, and it is causing me problems. I'm trying. Honest!
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?

Also, I frequently use something like s/^[A-Z]/~&/ to pre-pend a tilde or some other string to the beginning of the matched string. I know how to find the matched string, but I don't know how to change the beginning of it while still keeping the matched part.

If I were able to stay in the *nix environment for all my work, I could do it with these tools and the beloved pipe(|), but that isn't my lot in life. I would do it in Perl, but, frankly, it gives me headaches even looking at it.


You can get gnu Windows versions of awk sed and most other suchlike
goodies off the net ...

Jul 18 '05 #3

P: n/a
Or, much nicer
if line[:5]=='start': printing=1
if line.startswith('start'): printing=1
if line[:3]=='end': printing=0


if line.endswith('end'): printing=0

--
damjan
Jul 18 '05 #4

P: n/a
>>>>> "Damjan" == Damjan <gd*****@gmail.com> writes:

Damjan> Or, much nicer
if line[:5]=='start': printing=1
Damjan> if line.startswith('start'): printing=1
if line[:3]=='end': printing=0


Damjan> if line.endswith('end'): printing=0

No, it's still line.startswith('end'), not endswith.

--
Ville Vainio http://tinyurl.com/2prnb
Jul 18 '05 #5

P: n/a
>>>>> "John" == John Machin <sj******@lexicon.net> writes:

John> You can get gnu Windows versions of awk sed and most other
John> suchlike goodies off the net ...

Yeah, google for 'unxutils'. Cygwin versions of these tools can be a
headache sometimes.

--
Ville Vainio http://tinyurl.com/2prnb
Jul 18 '05 #6

P: n/a
On Wed, 16 Mar 2005 19:06:40 -0600, rumours say that Terry Hancock
<ha*****@anansispaceworks.com> might have written:

<snip>
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".
<snip>
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
suggested order to mimic sed functionality:
if line[:5]=='start': printing=1
if printing: print line
if line[:3]=='end': printing=0


and perhaps it's better to use startswith than slicing.
--
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
Jul 18 '05 #7

P: n/a
Kotlin Sam wrote:
Also, I frequently use something like s/^[A-Z]/~&/ to pre-pend a
tilde or some other string to the beginning of the matched string. I
know how to find the matched string, but I don't know how to change the
beginning of it while still keeping the matched part.


Something like
re.sub(r'^([A-Z])', r'~\1', target)
should do it.

Kent
Jul 18 '05 #8

P: n/a
Thanks to everyone who answered my two questions. I have only submitted
questions twice, and on both occasions the solutions were excellent,
and, I'm emarrassed to say, much simpler than I thought they would be.

My next goal is to be able to help someone they way y'all have helped me.

Thanks again,
Lance

Kent Johnson wrote:
Kotlin Sam wrote:
Also, I frequently use something like s/^[A-Z]/~&/ to pre-pend a
tilde or some other string to the beginning of the matched string. I
know how to find the matched string, but I don't know how to change
the beginning of it while still keeping the matched part.

Something like
re.sub(r'^([A-Z])', r'~\1', target)
should do it.

Kent


Jul 18 '05 #9

P: n/a
On Thu, 17 Mar 2005 18:37:11 -0500, Kotlin Sam <xa***********@hotmail.com> wrote:
Thanks to everyone who answered my two questions. I have only submitted
questions twice, and on both occasions the solutions were excellent,
and, I'm emarrassed to say, much simpler than I thought they would be.

My next goal is to be able to help someone they way y'all have helped me.
Bravo. That's the spirit.
For even better appreciation of your future efforts, consider not top-posting ;-)
Thanks again,
Lance

Kent Johnson wrote:
Kotlin Sam wrote:
Also, I frequently use something like s/^[A-Z]/~&/ to pre-pend a
tilde or some other string to the beginning of the matched string. I
know how to find the matched string, but I don't know how to change
the beginning of it while still keeping the matched part.

Something like
re.sub(r'^([A-Z])', r'~\1', target)
should do it.

Kent


Regards,
Bengt Richter
Jul 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.