Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix 用于分析/打印流的Shell命令 具体问题_Unix_Grep_Posix - Fatal编程技术网

Unix 用于分析/打印流的Shell命令 具体问题

Unix 用于分析/打印流的Shell命令 具体问题,unix,grep,posix,Unix,Grep,Posix,什么是shell命令来像这样转换字符串 class A(B, C): B -> A; C -> A; 像这样的字符串集合 class A(B, C): B -> A; C -> A; 其中A、B和C都是w+形式,而我写的“B、C”实际上是指由逗号和空格分隔的任意数量的术语。也就是说,“B,C”可以等同于“B”或“B,C,D,e” 全局 我正在可视化Python项目的类层次结构。我正在查找所有.py文件的目录,对类声明进行grepping,然后将它们转换为。到目

什么是shell命令来像这样转换字符串

class A(B, C):
B -> A; 
C -> A;
像这样的字符串集合

class A(B, C):
B -> A; 
C -> A;
其中A、B和C都是w+形式,而我写的“B、C”实际上是指由逗号和空格分隔的任意数量的术语。也就是说,“B,C”可以等同于“B”或“B,C,D,e”

全局
我正在可视化Python项目的类层次结构。我正在查找所有.py文件的目录,对类声明进行grepping,然后将它们转换为。到目前为止,我已经使用find和grep来获取行列表。我做过上面提到的事。如果可能的话,我只想使用标准的unix工具链。理想情况下,我希望找到另一个可组合的工具,通过管道进出并完成链

你想要原始的吗?自V7以来,这个sed脚本应该可以在每个UNIX上运行(但我还没有在任何真正旧的系统上测试过它,所以要小心)。将其作为
sed-n-f脚本文件infle>outfile运行

: loop
/^class [A-Za-z0-9_][A-Za-z0-9_]*(\([A-Za-z0-9_][A-Za-z0-9_]*, *\)*[A-Za-z0-9_][A-Za-z0-9_]*):$/{
h
s/^class \([A-Za-z0-9_][A-Za-z0-9_]*\)(\([A-Za-z0-9_][A-Za-z0-9_]*\)[,)].*/\2 -> \1;/
p
g
s/\(class [A-Za-z0-9_][A-Za-z0-9_]*(\)[A-Za-z0-9_][A-Za-z0-9_]*,* */\1/
b loop
}
这些是BRE(基本正则表达式)。它们没有
+
运算符(只有在扩展正则表达式中才能找到),而且它们肯定没有
\w
(由perl发明)。所以你简单的
\w+
变成了
[A-Za-z0-9][A-Za-z0-9][A-Za-z0-9]*
,我不得不多次使用它,结果造成了严重的丑陋

在伪代码形式中,它所做的是:

while the line matches /^class \w+(comma-separated-list-of \w+):$/ {
    save the line in the hold space
    capture the outer \w and the first \w in the parentheses
    replace the entire line with the new string "\2 -> \1;" using the captures
    print the line
    retrieve the line from the hold space
    delete the first member of the comma-separated list
}
使用Python的模块解析Python也很容易

导入ast
类类转储程序(ast.NodeVisitor):
def访问_ClassDef(自我,课堂):
def扩展_名称(expr):
如果isinstance(expr,ast.Name):
返回表达式id
如果isinstance(expr,ast.Attribute):
返回“%s.%s%”(扩展名称(扩展值),扩展属性)
返回ast.dump(expr)
对于clazz.bases中的base:
打印“%s->%s;”(clazz.name,展开_name(基本))
ClassDumper.generic_访问(self,clazz)
ClassDumper().visit(ast.parse(open(_文件__).read()))

(这不是完全正确的w.r.t.嵌套,因为它将输出
internal->Base;
而不是
Outer.internal->Base;
,但您可以通过手动遍历跟踪上下文来解决这一问题。)

Python现在在UNIX系统上相当标准。除非你希望在路由器、电视或其他设备上运行,否则你可能对现有设备很在行。我还是想看看是否有标准的解决方案。这是因为(1)不必切换到Python会很好(2)我发现了解这些工具确实会提高我的工作效率,尽管我有一种强大的脚本语言可供使用。Python和其他任何东西一样都是“标准”解决方案。和其他人一样,它也是一名口译员。你的选择是一个整体,而不是更多的灵长类。Sed和Awk都很古老,它们都可能让你达到目的。Perl是另一种选择,它只比awk更新一点,而且肯定会更快。您可能可以用Bash编写,但用sh编写就不那么容易了。。。不管那意味着什么。