4 bash recursion awk circular-dependency
使用此文件,我想打印一个包依赖关系树,给定一个基本包.例如,使用Bash包
@ bash
# few lines removed
requires: coreutils libintl8 libncursesw10 libreadline7 _update-info-dir cygwin
Run Code Online (Sandbox Code Playgroud)
我想找到所需包的输出,部分示例
bash
bash coreutils
bash coreutils libattr1
bash coreutils libattr1 libintl8
bash coreutils libattr1 libintl8 libiconv2
bash coreutils libattr1 libintl8 _autorebase
bash coreutils libattr1 libintl8 _autorebase rebase
bash coreutils libattr1 libintl8 _autorebase rebase dash
bash coreutils libattr1 libintl8 _autorebase rebase dash cygwin
bash coreutils libattr1 libintl8 _autorebase rebase dash cygwin base-cygwin
Run Code Online (Sandbox Code Playgroud)
我有这个命令,但它没有递归
#!awk -f
$1 == "@" {
pkg = $2
}
$1 == "requires:" {
for (i=2; i<=NF; i++)
reqs[pkg][i-1] = $i
}
END {
query = "bash"
for (pkg in reqs[query]) {
print reqs[query][pkg]
}
}
Run Code Online (Sandbox Code Playgroud)
使用Perl并且没有评论:
perl -lne '
$k = $1 if /@\s*(\S+)/;
@r=split(); shift @r; $r{$k} = [@r] if /requires:/;
END{
$p = "bash"; @l = ( [$p, 0] );
while ($p = pop @l) {
next if $d{$p->[0]}++;
print " " x $p->[1] . $p->[0];
for $d(@{$r{$p->[0]}}) {
push @l, [ $d, $p->[1]+1 ];
}
}
}' setup.ini
Run Code Online (Sandbox Code Playgroud)
Awk版本:
awk '/^@ / { split($0, b); k = b[2]; }
/^requires: / { a[k] = $0; }
END {
p[1] = "bash"; d["bash"] = 0;
while (length(p)) {
key = p[length(p)]; depth = d[key]; delete p[length(p)];
if (!s[key]++) {
printf "%*s %s\n", depth, "", key;
split(a[key], r); delete r[1];
for (req in r) {
p[length(p) + 1] = r[req]; d[r[req]] = depth + 1;
}
}
}
}
' setup.ini
Run Code Online (Sandbox Code Playgroud)
小智 0
#!/usr/bin/awk -f
@include "join"
$1 == "@" {
apg = $2
}
$1 == "requires:" {
for (z=2; z<=NF; z++)
reqs[apg][z-1] = $z
}
END {
prpg("bash")
}
function smartmatch(small, large, values) {
for (each in large)
values[large[each]]
return small in values
}
function prpg(fpg) {
if (smartmatch(fpg, spath)) return
spath[length(spath)+1] = fpg
print join(spath, 1, length(spath))
if (isarray(reqs[fpg]))
for (each in reqs[fpg])
prpg(reqs[fpg][each])
delete spath[length(spath)]
}
Run Code Online (Sandbox Code Playgroud)