Thursday, December 24, 2009

Re: Dupicate and increment lines

epanda wrote:
>
> On 24 déc, 15:14, Tim Chase <v...@tim.thechases.com> wrote:
>>> Goal : multiply some parts of lines which contain factors
>>> foo(2)bar(9)
>>> Should become
>>> foo1bar1
>>> foo1bar2
>> ...
>>> foo1bar9
>>> foo2bar1
>>> foo2bar2
>> ...
>>> foo2bar9

For something close, you can use the following python code that is
pretty speedy and should handle arbitrary counts of repeats, and pad in
the optionally missing bits with "1"

###########################################
import re
r = re.compile(r'\((\d+)\)')
def printall(leader, bits):
if bits and bits[0].strip():
text = bits[0]
try:
times = int(bits[1])
except:
times = 1
for i in range(times):
printall(
'%s%s%i' % (leader, text, i+1),
bits[2:]
)
else:
print leader
for line in file('epanda.txt'):
line = line.rstrip('\n')
if r.search(line):
printall('', r.split(line))
else:
print line

###########################################

You can tweak the behavior for those lines that have no count so they
behave as you want.

-tim

--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment