[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: quick scripting question - finding occurrence in many lines



On Sunday 05 November 2006 09:03, Ken Irving wrote:
> On Fri, Nov 03, 2006 at 09:56:12PM -0500, Douglas Tutty wrote:
> > On Fri, Nov 03, 2006 at 08:27:42PM +0000, michael wrote:

[...]

> > > eg for
> > >
> > > junk info 18 Pro
> > > cessor
> > >
> > > I wish to get the field '18'

[...]

> >
> > Since it appears that newlines aren't significant, I would get rid of
> > them.
> >
> > 	IN = open('IN')
> > 	instring = IN.read()
> > 	IN.close()
> >
> > I would remove all newlines so it was one huge line.
> >
> > 	onelinestring = instring.replace('\n', ' ')
> > 	del instring
> >
> > Split the string into a list of words
> >
> > 	inlist = onelinestring.split()
> > 	del onelinestring
> >
> > Iterate through the list looking for 'processor'
> >
> > 	oldword = ' '
> > 	for newword in inlist
> > 		if word.lower == 'processor'
> > 			print oldword	# the previous word
> > 		oldword = newword
> >
> > 	del inlist
> >

[...]

>
> Is this pseudo-code or does it actually run?  I had to add some crypic
> noise, I mean ':' characters, in a couple of places, change "word" to
> "newword", and it still didn't seem to work.  The interesting part of
> the otherwise mundane problem was that the pattern to match is perhaps
> on two different lines.  I don't see how this is addressed in the
> proffered solution.

[...]

Here's a version of Douglas' python script that I got to run:

--------------------------------------------

#!/usr/bin/python 

IN = open('IN')
instring = IN.read()

onelinestring = instring.replace('\n', ' ')
 
inlist = onelinestring.split()

oldword = ' '

for newword in inlist:
 
	if newword == 'Processor':
		print oldword	
	oldword = newword
-------------------------------------------------------------

There were a couple of syntax errors in the original, but I think it does 
solve the two-line problem by removing the newlines.

Regards,

John



Reply to: