Page 1 of 1

Search and replace issue

Posted: Wed Oct 01, 2003 7:22 pm
by jbloggs2002
I would like to be able search and replace in a file, using search terms taken from another file.
Specifically, I have two very long lists, and I would like to remove all instances contained in one list from the other.

Any help gratefully received

Posted: Wed Oct 01, 2003 7:43 pm
by s_reynisson
Look in TP's help file, search for "How to Sort by Selected Column"
or "How to Sort by Specified Key Positions".

Posted: Wed Oct 01, 2003 8:01 pm
by jbloggs2002
I don't understand how this will help me search and replace.

Posted: Wed Oct 01, 2003 8:12 pm
by s_reynisson
I was hoping you would be able to use the "Delete duplicate lines".
But, I see that TP uses the entire line when it decides what to delete,
not just the key or the block selected.
I can feel an enhancement request brewing.

Posted: Wed Oct 01, 2003 8:17 pm
by jbloggs2002
"delete duplicate lines" also only deletes one of the lines.
Do you know of any other product that could do this?

Posted: Wed Oct 01, 2003 8:24 pm
by s_reynisson
All I can think of is MS Access or Word/Excel. But using Word or Excel
needs coding, not sure about Access. You can also find some scripts on
Google etc.

Posted: Wed Oct 01, 2003 11:00 pm
by talleyrand
Bah, why code when you can have some else do it for you?
Install Python and you're good to go.

file1.txt
a
b
c
d
e
f
goo
hoo
lou

file2.txt
aribba
bob
c
larry
dude
e
f
goo
hoo
louisa

file3.txt
aribba
bob
larry
dude
louisa

Copy this code and paste into Textpad.
Save it to something like C:\removeTerms.py
Update the paths for file1, file2 and file3
C:\>python removeTerms.py

Code: Select all

#keywords exist in this one
file1 = r"c:\bfellows\file1.txt"

#this is the file being compared
file2 = r"c:\bfellows\file2.txt"

#store the results in this file
file3 = r"c:\bfellows\file3.txt"

f1 = open(file1, 'r').readlines()
f2 = open(file2, 'r').readlines()


l = []
for keep in f2:
   #build a new list of everything that exists in f2 that is not in f1
   if (keep not in f1):
      l.append(keep)

#build up a big string to write the data out
bigString = ""
for x in l:
   bigString += x

#create a file and store the data in it
try:
   output = open(file3, 'w')
   output.writelines(bigString)
finally:
   output.close()

Posted: Wed Oct 01, 2003 11:39 pm
by s_reynisson
Or you can just wait a few sec's for talleyrand to wake up! :D
s_reynisson wrote:... You can also find some scripts on Google...

Posted: Wed Oct 01, 2003 11:39 pm
by jbloggs2002
Dear Mr. Talleyrand,

You have now officially become my hero. I have erected a small shrine to you in the corner of my cubicle, and I shall worship there at 9am and 3pm MST each day. The taunts of my co-workers shall only make me more devout.

Thanks!

Posted: Thu Oct 02, 2003 2:05 am
by talleyrand
You can pass on the 8 CST o'clock worship session. I'm eating my Wheaties and rubbing the sleep out of my eyes and therefore will not be receptive to petitions. ;) However, if you do lots of little things like that, you should check out Python. It's a great little language to do lots of stuff. TextPad's awesome for it.