-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace_last
executable file
·66 lines (52 loc) · 1.89 KB
/
replace_last
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/python
"""
replace_last: Replace the last match in a file
Copyright (c) 2009, Richard Clark, Red Spider Limited <[email protected]>
Contributers include:
Stephen J (Some script boilerplate code adopted from his NZPUG presentation)
See LICENSE file for details.
"""
import os, sys, random, logging, re
from optparse import OptionParser
WHENCE_RELATIVE = 1
logging.basicConfig(level=logging.WARN)
log = logging.getLogger('Skim')
def replace_last_generator(match, replace, file):
buffer = ""
rbuffer = None
for line in file:
if re.search(match, line):
yield buffer
if not rbuffer is None:
yield rbuffer
buffer = line
rbuffer = None
else:
if rbuffer is None:
rbuffer = line
else:
rbuffer += line
yield re.sub(r'(.*)%s(.*?)$' % match, '\\1' + replace + '\\2', buffer)
if not rbuffer is None:
yield rbuffer
def replace_last(match, replace, file):
""" Replace the last entry in the file """
for chunk in replace_last_generator(match, replace, file):
sys.stdout.write(chunk)
if __name__ == "__main__":
usage = "usage: %prog [options] match replace [filename]"
parser = OptionParser(usage="usage: %prog [options] match replace filename")
parser.add_option("--verbose","-v",
help = "print debugging output",
action = "store_true")
(options, args) = parser.parse_args()
if options.verbose:
log.setLevel(logging.DEBUG)
log.debug("Verbose mode: %s" % options.verbose)
(match, replace) = args[:2]
log.debug("Match: %s" % match)
log.debug("Replace: %s" % replace)
if len(args) > 2 and args[2] != '-':
replace_last(match, replace, open(args[2],'rb'))
else:
replace_last(match, replace, sys.stdin)