Using sed to interpret multiple lines on condition
Question: I'm stuck on constructing a **sed** expression that will parse a python file's
imports and extract the names of the modules.

This is a simple example that I solved using (I need the output to be the
module names without 'as' or any spaces..):

    
    
    from testfunctions import mod1, mod2 as blala, mod3, mod4
    

What I have so far:

    
    
    grep -ir "from testfunctions import" */*.py | sed -E s/'\s+as\s+\w+'//g | sed -E s/'from testfunctions import\s+'//g
    

This does get me the required result in a situation as above.

**The problem:** In files where the imports are like so:

    
    
    from testfunctions import mod1, mod2 as blala, mod3, mod4 \
         mod5, mod6 as bla, mod7 \
       mod8, mod9 ...
    

**Any ideas how I can improve my piped expression to handle multiple lines?**


Answer: Try this;

    
    
       sed -n -r '/from/,/^\s*$/p;' *.py | sed ':x; /\\$/ { N; s/\\\n//; tx }'  | sed 's/^.*.import//g;s/  */ /g'
    

