[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 16:42, John O'Hagan wrote:
> 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'
>
[...]

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

Or, now that I've seen Ken's contribution:

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

#!/usr/bin/python 

for newword in open('IN').read().replace('\n', '').split():
 
	if newword == 'Processor':
		print oldword	
	oldword = newword

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

Or in bash:

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

#!/bin/bash

for newword in $(sed s/\\n//g < IN); do

	[[ $newword == "Processor" ]] && echo $oldword
	oldword=$newword
	
done

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

Either way, I like Douglas' approach of removing the newlines - or perhaps 
these loops are inefficient?

Regards,

John



Reply to: