I can do it in sed...

K

Kotlin Sam

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
 
T

Terry Hancock

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
 
J

John Machin

Kotlin said:
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 ...
 
V

Ville Vainio

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.
 
C

Christos TZOTZIOY Georgiou

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".

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.
 
K

Kent Johnson

Kotlin said:
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
 
K

Kotlin Sam

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 said:
Kotlin said:
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
 
B

Bengt Richter

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 said:
Kotlin said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top