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


Ummm ... what's wrong with Python?



lineIter= iter(aFile)
for aLine in lineIter:
    if aLine.startswith("import"):
        if aLine.endswith("("):
            for aModule in lineIter:
                if aModule.endwith(")"):
                    break
                print "import", aModule.strip()
        else:
            print aLine.stri()
    else:
        print aLine.strip()

      

+1


source


This might work for you:



sed '/^from/,/^)/{H;//{x;/)/{s/[\n()]//g;s/  */ /g;s/,$//;p;x}};d}' source
from XXX import AAA, BBB
from YYY import CCC, DDD, EEE, ...
...other instructions...

      

+2


source


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







All Articles