如何获取 awk 打印函数名称和注释
How to get awk print function name and comment
你好 awk ninjas 我需要一些帮助,所以我找到了一个解决方案,可以从 here 的 shell 脚本中打印定义的函数名称,但我的问题是我希望我的函数能够显示注释。 .. 这是我的函数定义方式的示例。
function1() { # this is a function 1
command1
}
function2() { # this is a function 2
command2
}
function3() { # this is a function 3
command3
}
所以提供的命令是这个:typeset -f | awk '/ \(\) $/ && !/^main / {print }'
输出将是这样的:
function1
function2
function3
但我希望输出是这样的(函数名称和注释之间的 4 space 也非常好):
function1 # this is a function 1
function2 # this is a function 2
function3 # this is a function 3
提前致谢。
作为答案写作以获得更好的格式化能力。
您可以尝试使用自定义的“虚拟”变量并在其中存储评论。
让我们声明我们的函数:
test_function () {
__COMMENT="TEST_FUNCTION"
}
我使用 __COMMENT
变量作为我们的占位符。请注意,bash
将在 2 行中打印函数名称和变量,因此我们必须在 awk
代码中使用状态机:
typeset -f | awk \
'match([=11=], /__COMMENT="[^"]*/) {
if (functionname) {
print functionname ": " substr([=11=], RSTART+11, RLENGTH-11)
}
}
/ \(\) {$/ && !/^main / {functionname=}'
从末尾开始:
/ \(\) {$/ && !/^main / {functionname=}
我不得不修改你的模式以匹配我的输出,所以你可能需要删除大括号字符以使其在你这边工作。它将查找函数声明并将其名称存储为 functionname
.
'match([=13=], /__COMMENT="[^"]*/) {
它会寻找我们的变量。注意,__COMMENT="
的长度是 11
。我们需要它。
if (functionname) {
print functionname ": " substr([=14=], RSTART+11, RLENGTH-11)
}
如果设置了 functionname
,它会打印我们匹配的名称和子字符串,从开头跳过 11 个字符,从结尾跳过 1 个字符(引号)。
尝试通过此管道发送输出:
grep '() ' | grep -v '^main' | sed '/() { /s// /'
解释:第一个 grep
查找包含带有 ()
的函数定义的所有行。第二个过滤掉 main()
函数。最后,sed
命令将字符串 () {
替换为四个空格。
出于测试目的,我将您上面描述的输入放入一个文件 s.txt
,其中包含:
function1() { # this is a function 1
command1
}
function2() { # this is a function 2
command2
}
function3() { # this is a function 3
command3
}
main() { # this is the main function
commands
}
当我运行
cat s.txt | grep '() ' | grep -v '^main' | sed '/() { /s// /'
我得到这个输出:
function1 # this is a function 1
function2 # this is a function 2
function3 # this is a function 3
将每个 #
替换为 :
:
function1() { : this is a function 1
command1
}
function2() { : this is a function 2
command2
}
function3() { : this is a function 3
command3
}
$ typeset -f | awk '/ \() $/{fn=} /^ *:/{ print fn [=11=] }'
function1 : this is a function 1;
function2 : this is a function 2;
function3 : this is a function 3;
根据需要更改 awk 脚本以匹配您拥有的任何其他功能布局and/or根据您的喜好调整输出。
有关详细信息,请参阅 What is the purpose of the : (colon) GNU Bash builtin?。
你好 awk ninjas 我需要一些帮助,所以我找到了一个解决方案,可以从 here 的 shell 脚本中打印定义的函数名称,但我的问题是我希望我的函数能够显示注释。 .. 这是我的函数定义方式的示例。
function1() { # this is a function 1
command1
}
function2() { # this is a function 2
command2
}
function3() { # this is a function 3
command3
}
所以提供的命令是这个:typeset -f | awk '/ \(\) $/ && !/^main / {print }'
输出将是这样的:
function1
function2
function3
但我希望输出是这样的(函数名称和注释之间的 4 space 也非常好):
function1 # this is a function 1
function2 # this is a function 2
function3 # this is a function 3
提前致谢。
作为答案写作以获得更好的格式化能力。 您可以尝试使用自定义的“虚拟”变量并在其中存储评论。
让我们声明我们的函数:
test_function () {
__COMMENT="TEST_FUNCTION"
}
我使用 __COMMENT
变量作为我们的占位符。请注意,bash
将在 2 行中打印函数名称和变量,因此我们必须在 awk
代码中使用状态机:
typeset -f | awk \
'match([=11=], /__COMMENT="[^"]*/) {
if (functionname) {
print functionname ": " substr([=11=], RSTART+11, RLENGTH-11)
}
}
/ \(\) {$/ && !/^main / {functionname=}'
从末尾开始:
/ \(\) {$/ && !/^main / {functionname=}
我不得不修改你的模式以匹配我的输出,所以你可能需要删除大括号字符以使其在你这边工作。它将查找函数声明并将其名称存储为 functionname
.
'match([=13=], /__COMMENT="[^"]*/) {
它会寻找我们的变量。注意,__COMMENT="
的长度是 11
。我们需要它。
if (functionname) {
print functionname ": " substr([=14=], RSTART+11, RLENGTH-11)
}
如果设置了 functionname
,它会打印我们匹配的名称和子字符串,从开头跳过 11 个字符,从结尾跳过 1 个字符(引号)。
尝试通过此管道发送输出:
grep '() ' | grep -v '^main' | sed '/() { /s// /'
解释:第一个 grep
查找包含带有 ()
的函数定义的所有行。第二个过滤掉 main()
函数。最后,sed
命令将字符串 () {
替换为四个空格。
出于测试目的,我将您上面描述的输入放入一个文件 s.txt
,其中包含:
function1() { # this is a function 1
command1
}
function2() { # this is a function 2
command2
}
function3() { # this is a function 3
command3
}
main() { # this is the main function
commands
}
当我运行
cat s.txt | grep '() ' | grep -v '^main' | sed '/() { /s// /'
我得到这个输出:
function1 # this is a function 1
function2 # this is a function 2
function3 # this is a function 3
将每个 #
替换为 :
:
function1() { : this is a function 1
command1
}
function2() { : this is a function 2
command2
}
function3() { : this is a function 3
command3
}
$ typeset -f | awk '/ \() $/{fn=} /^ *:/{ print fn [=11=] }'
function1 : this is a function 1;
function2 : this is a function 2;
function3 : this is a function 3;
根据需要更改 awk 脚本以匹配您拥有的任何其他功能布局and/or根据您的喜好调整输出。
有关详细信息,请参阅 What is the purpose of the : (colon) GNU Bash builtin?。