How do I perform multi-line search and replace using a script?
I am trying to replace every multi-line import inside a Python source file. So the source looks like
from XXX import (
AAA,
BBB,
)
from YYY import (
CCC,
DDD,
EEE,
...
)
...other instructions...
and I would like to get something like
from XXX import AAA, BBB
from YYY import CCC, DDD, EEE, ...
...other instructions...
I tried using sed, but it looks like it doesn't support non-living matching of the closing parenthesis, so it "eats" the second import. :( Any hint? This isn't possible with sed? Should I try another tool?
+1
source to share
3 answers
For posterity, here is a somewhat polished version of the S.Lott script (I would post it as a comment, but too long ^^;). This version preserves the indentation and gives a result closer to my example.
lineIter = iter (aFile) for aLine in lineIter: s = aLine.strip () if s.startswith ("from") and s.endswith ("("): complete = s [: - 1] for aModule in lineIter: m = aModule.strip () if m.endswith (")"): break complete + = m.strip () print complete else: print aLine,
+1
source to share