简体   繁体   English

使用sed在条件下解释多行

[英]Using sed to interpret multiple lines on condition

I'm stuck on constructing a sed expression that will parse a python file's imports and extract the names of the modules. 我坚持构造一个sed表达式,该表达式将解析python文件的导入并提取模块的名称。

This is a simple example that I solved using (I need the output to be the module names without 'as' or any spaces..): 这是我解决的一个简单示例(我需要将输出作为没有'as'或任何空格的模块名称。):

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? 有什么想法可以改善管道表达式来处理多行吗?

尝试这个;

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

Thanks everyone for your help. 感谢大家的帮助。 I didn't know a module such as ast exists.. It really helped me achieve my goal. 我不知道诸如ast这样的模块。.它确实帮助我实现了目标。

I put together a simple version of the solution I needed, just for reference if anyone else encounters this question as well: 我整理了所需解决方案的简单版本,以供其他人也遇到此问题时提供参考:

import glob
import ast

moduleList = []
# get all .py file names
testFiles = glob.glob('*/*.py')
for testFile in testFiles:
    with open(testFile) as code:
        # ast.parse creates the tree off of plain code
        tree = ast.parse(code.read())
        # there are better ways to traverse the tree, in this sample there
        # is no guarantee to the traversal order
        for node in ast.walk(tree):
            if isinstance(node, ast.ImportFrom) and node.module == 'testfunctions':
                # each node will contain an ast.ImportFrom instance which
                # data members are: module, names(list of ast.alias) and level
                moduleList.extend([alias.name for alias in node.names])

You can read more about it in (probably the only detailed page about ast in the whole web) here: https://greentreesnakes.readthedocs.io/en/latest/manipulating.html#inspecting-nodes 您可以在以下网站(可能是整个网络中有关ast的唯一详细页面)上了解有关此内容的更多信息: https : //greentreesnakes.readthedocs.io/en/latest/manipulating.html#inspecting-nodes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM