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 9 1804
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
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 ...
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
>>>>> "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
>>>>> "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
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...
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: William C. White |
last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using
cURL? Our website is hosted on a shared drive and the webhost company
doesn't installed additional software (such as cURL)...
|
by: Albert Ahtenberg |
last post by:
Hello,
I don't know if it is only me but I was sure that header("Location:url")
redirects the browser instantly to URL, or at least stops the execution of
the code. But appearantely it continues...
|
by: James |
last post by:
Hi,
I have a form with 2 fields.
'A'
'B'
The user completes one of the fields and the form is submitted.
On the results page I want to run a query, but this will change
subject to which...
|
by: Ollivier Robert |
last post by:
Hello,
I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9
system. The link succeeds but everytime I try to run php, I get a SEGV from
inside the libcnltsh.so library.
...
|
by: Richard Galli |
last post by:
I want viewers to compare state laws on a single subject.
Imagine a three-column table with a drop-down box on the top. A viewer
selects a state from the list, and that state's text fills the...
|
by: Albert Ahtenberg |
last post by:
Hello,
I have two questions.
1. When the user presses the back button and returns to a form he filled
the form is reseted. How do I leave there the values he inserted?
2. When the...
|
by: inderjit S Gabrie |
last post by:
Hi all
Here is the scenerio ...is it possibly to do this...
i am getting valid course dates output on to a web which i have designed
....all is okay so far , look at the following web url
...
|
by: Jack |
last post by:
Hi All,
What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g.
select x from y where z=:parameter
Which in asp/jsp would be followed by some statements to bind a value...
|
by: Sandwick |
last post by:
I am trying to change the size of a drawing so they are all 3x3.
the script below is what i was trying to use to cut it in half ... I
get errors.
I can display the normal picture but not the...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |