我正在查看一个日志文件,该文件仅告诉我该文件中具有错误的文件名和行号。我感兴趣的是了解封装功能。例如,这是日志文件的内容
Error: foo.file on line wxy
Error: foo.file on line xyz
.
.
.
这是文件foo.file的内容
function abc_1234 (...)
.
.
.
endfunction
function def_442 ()
.
.
.
//Following line number is WXY
assign Z ==== X;
endfunction
function ghi(...)
.
.
.
//Following line number is XYZ
assign X = X;
endfunction
.
.
.
基于上述日志文件,我想获取返回的函数名称def
和ghi
。我尝试了@larsks提供的部分解决方案,并添加了[[::blank::]]
# look for function definitions and record the function name
# in the func_name variable
/function [[:alpha:]][[:alnum:]]*[[:blank:]]*([^)]*)/ {
func_name = substr($2, 1, index($2, "(")-1);
}
# when we reach the target line number, print out the current
# value of func_name
NR == target {
print func_name
}
在abc_1234 (...)
和def_442 (...)
上失败,因为(
前面有一个空格。我无法使以上内容正常工作
答案 0 :(得分:2)
为了将行号映射到函数定义,您将需要遍历源文件以查找函数定义,然后在遇到目标行号时将其打印出来。例如,如下所示:
# look for function definitions and record the function name
# in the func_name variable. This looks for lines matching the pattern
# function <space> <identifier>(<anything>), and records the
# <identifier> part in func_name.
/function [[:alpha:]][[:alnum:]]* *([^)]*)/ {
func_name = $0
func_name = gensub("function *", "", 1, func_name)
func_name = gensub(" *\\(.*", "", 1, func_name)
}
# when we reach the target line number, print out the current
# value of func_name. In awk, the variable NR represents the
# current line number, and target is a variable we expect to be
# passed in on the command line.
NR == target {
print func_name
}
如果将其放在名为findline.awk
的文件中,并按以下方式调用它:
awk -f findline.awk -vtarget=26 mysourcefile.src
然后它将打印出包含第26行的函数的名称。此脚本编写的并不是很健壮,但希望它能为您提供一些有关如何进行操作的想法。
有关gensub
函数的详细信息,请参见awk documentation。
答案 1 :(得分:2)
您可以尝试以下Perl解决方案
$ perl -0777 -ne ' while( /function\s+(\w+).+?endfunction/sg) { print "$1\n" } ' tulamba.log
abc_1234
def_442
ghi
$