这就是我需要做的:
输入:(Python)
## This is a function,
## its purpose is... yada yada yada
def function_name(x):
return x + 1产出:(减价)
## function_name
This is a function,
its purpose is... yada yada yada到目前为止我得到了:
sed -n '/## /,/def/ { /## \|def/ p }' TEST.py | cut -d' ' -f2- | sed 's/(.*)\(.*\)://'它产生:
This is a function,
its purpose is... yada yada yada
function_name有两个条件:
1.-不匹配这些模式的"##“和"def”之间的行应该被忽略。
示例:
输入:(Python)
## This is a function,
## its purpose is... yada yada yada
# This is a normal comment, nothing to see here! (ignored)
def function_name(x):
return x + 1产出:(减价)
## function_name
This is a function,
its purpose is... yada yada yada2.-第二种模式必须加以推广,例如,不要使用"def",比方说我要使用"class“。
输入:(Python)
## This is a class,
## its purpose is... yada yada yada
# This is a normal comment, nothing to see here! (ignored)
class class_name:
__init__(self, x):
self.x = x产出:(减价)
## class_name
This is a class,
its purpose is... yada yada yada发布于 2021-05-19 22:01:18
使用awk
$ awk '/^## / { comment = comment substr($0, 4) "\n" }
/^def / { printf "## %s\n%s", substr($0, 5, index($0, "(") - 5), comment
comment = "" }' TEST.py
## function_name
This is a function,
its purpose is... yada yada yada将以##开头的所有行追加到变量中,当它看到以def开头的行时,打印函数名和前面行的变量,然后空白该变量以重新启动。
使用持有空间存储注释行的GNU sed版本:
$ sed -n -e '/^## / { s/^## //; H }' -e '/^def / { s/^def \([^(]*\).*/## \1/; G; s/\n\n/\n/; p; z; x }' TEST.py
## function_name
This is a function,
its purpose is... yada yada yada发布于 2021-05-19 23:36:17
在每个Unix框上使用任何shell中的任何awk (如果awk不支持POSIX字符类,则使用a-zA-Z0-9而不是[:alnum:] ):
$ cat tst.awk
sub(/^## */,"") {
cmts = cmts $0 ORS
next
}
sub(/^[[:alnum:]_]+ */,"") {
sub(/[^[:alnum:]_].*/,"")
print "##", $0
print cmts
cmts = ""
}$ awk -f tst.awk file
## function_name
This is a function,
its purpose is... yada yada yada
## function_name
This is a function,
its purpose is... yada yada yada
## class_name
This is a class,
its purpose is... yada yada yada上面的内容是在这个输入文件上运行的:
$ cat file
## This is a function,
## its purpose is... yada yada yada
def function_name(x):
return x + 1
## This is a function,
## its purpose is... yada yada yada
# This is a normal comment, nothing to see here! (ignored)
def function_name(x):
return x + 1
## This is a class,
## its purpose is... yada yada yada
# This is a normal comment, nothing to see here! (ignored)
class class_name:
__init__(self, x):
self.x = x发布于 2021-05-19 22:09:21
awk '
/##/ {
gsub( "## *", "" )
previous[ ++lines ] = $0
}
/def/ {
gsub( "def ", "")
gsub( "[(].*","" )
print "## " $0
for (x = 1 ; x <= lines ; x++ )
print previous[x]
lines = 0
}
' TEST.py看来肖恩比我早找到答案了。答案是相似的,它们的区别主要在于线条是如何累积的。Shawn将前面的行累加在一个变量(注释)中,而我则将其累加到数组中。
我认为Shawn在代码中可能有更多的假设,就像每个##在它之后都有一个空格,或者函数名从一行的第五个字符开始,而##和def都出现在行的开头。
我认为我也应该添加一些锚,因为/def/可能有很多意想不到的匹配,所以/^def/可能要好得多。
有趣的是,我们都忽略了使用sed,因为我个人不知道如何处理sed中的变量。我感兴趣的是,其他解决方案是否可以从纯sed社区获得。
https://stackoverflow.com/questions/67611344
复制相似问题