2133 |
paro |
1 |
#!/usr/bin/python |
|
|
2 |
import os, sys |
|
|
3 |
usage = "usage: %s search_text replace_text [infile [outfile]]" % os.path.basename(sys.argv[0]) |
|
|
4 |
|
|
|
5 |
if len(sys.argv)<3: |
|
|
6 |
print usage |
|
|
7 |
|
|
|
8 |
else: |
|
|
9 |
stext = sys.argv[1] |
|
|
10 |
rtext = sys.argv[2] |
|
|
11 |
input = sys.stdin |
|
|
12 |
output = sys.stdout |
|
|
13 |
# print "There are %s args " %len(sys.argv) |
|
|
14 |
|
|
|
15 |
if len(sys.argv) > 3: |
|
|
16 |
input = open(sys.argv[3]) |
|
|
17 |
|
|
|
18 |
if len(sys.argv) > 4: |
|
|
19 |
output = open(sys.argv[4], 'w') |
|
|
20 |
|
|
|
21 |
for s in input: |
|
|
22 |
output.write(s.replace(stext, rtext)) |
|
|
23 |
|
|
|
24 |
if len(sys.argv)> 3: |
|
|
25 |
input.close() |
|
|
26 |
if len(sys.argv) > 4: |
|
|
27 |
output.close() |