Search and replace issue
Moderators: AmigoJack, bbadmin, helios, MudGuard
-
jbloggs2002
- Posts: 4
- Joined: Wed Oct 01, 2003 7:19 pm
Search and replace issue
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
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
- s_reynisson
- Posts: 939
- Joined: Tue May 06, 2003 1:59 pm
-
jbloggs2002
- Posts: 4
- Joined: Wed Oct 01, 2003 7:19 pm
- s_reynisson
- Posts: 939
- Joined: Tue May 06, 2003 1:59 pm
-
jbloggs2002
- Posts: 4
- Joined: Wed Oct 01, 2003 7:19 pm
- s_reynisson
- Posts: 939
- Joined: Tue May 06, 2003 1:59 pm
- talleyrand
- Posts: 624
- Joined: Mon Jul 21, 2003 6:56 pm
- Location: Kansas City, MO, USA
- Contact:
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
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()
I choose to fight with a sack of angry cats.
- s_reynisson
- Posts: 939
- Joined: Tue May 06, 2003 1:59 pm
-
jbloggs2002
- Posts: 4
- Joined: Wed Oct 01, 2003 7:19 pm
- talleyrand
- Posts: 624
- Joined: Mon Jul 21, 2003 6:56 pm
- Location: Kansas City, MO, USA
- Contact:
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.
I choose to fight with a sack of angry cats.