创建“树”结构时,递归搜索项目

问题描述 投票:0回答:0

I在Azure Devops上工作,在其中有一个模板的概念(类似于Bash中的功能)。因此,一个模板将使用其他模板执行一些操作。

大多数模板(任何其他模板都不使用的模板)是Extpemplates,文件名中的文件名称

extTemplate。 我正在尝试构建一种“树”的“树”(例如树命令),指示哪些模板在Extpemplates中使用,然后在这些模板中使用了哪些模板等。 i具有以下功能,可以递归查找模板引用(以文本

-模板开头的行总是调用其他模板:

函数的输入是extpemplates的数组(我们可以使用find命令获得)。添加点并删除“ “由于模板的调用方式,因此需要。 string_template() { array_of_files_to_search=("$@") for file_to_search in "${array_of_files_to_search[@]}"; do if [[ "$file_to_search" = '/'* ]]; then file_to_search=$(echo ".${file_to_search}" | tr -d '\n') fi if [[ $file_to_search = *"exttemplate"* ]]; then echo -e "\e[34mSearching in $file_to_search & its refereneced templates\e[0m" else echo -e "\e[33mSearching in $file_to_search & its refereneced templates\e[0m" fi readarray references < <(grep -E "^\s*\- template:.*" "$file_to_search" | sed 's/#.*//' | sed 's/- template://' | awk '{$1=$1;print}') if [ "${#references[@]}" -gt 0 ]; then string_template "${references[@]}" else echo "This $file_to_search does not use any other templates" fi done }

功能正常工作,并且找到了所有模板。但是,我无法完成的是生成树结构。

我现在得到的是下面的。由于Extpemplates具有唯一的名称,因此我可以识别它们并以不同的颜色打印它们。但这对于其他模板是不可能的。如何在递归时跟踪“级别”? 在PIC中(我有一些要说明的线路),末尾带有红色标记的线应为2级,蓝色标记的线应为3级(假设extPemplates在0级别为0)。如何实现跟踪“级别”?

基于评论

update

enter image description here问题您可以在不同树级的其他几个模板中使用相同的模板吗?如果是,您想做什么?在树上重复其所有子策略? 是的。如果发生这种情况,请在树上重复其所有子序列。

您可能只能用gnuawk来完成所有这些。以下假定您的文件名不包含newline字符,并且要启动的文件列表存储在文本文件中,每个行每行文件名。如果无法打开任何遇到的文件,它将带有错误消息。它打印了一个带有2个空间凹痕的简单树。如果您的示例确实需要额外的文本和ANSI代码,则需要适应。

$ cat foo.awk function walk(prefix, file) { printf("%s%s\n", prefix, file) for(f in a[f]) walk(" " prefix, f) } { files[$0] } END { while(length(files)) { for(f in files) { while ((e = getline < f) > 0) { if($0 ~ /^\s*- template:/) { gsub(/^\s*- template:\s*|\s*#.*/, "") $0 = ($0 ~ /^[/]/ ? "." : "") $0 if($0) a[f][$0] files[$0] } } if(e < 0) { print f ": file not found"; exit(1) } close(f) delete files[f] break } } for(f in a) if(f ~ /exttemplate/) walk("", f) } $ awk -f foo.awk files_to_search.txt foo.exttemplate b/bar.template b/2/bar b/1/bar a/3/bar a/foo.template b/3/foo c/2/foo a/1/foo

bash recursion
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.