4

我有这个 Makefile,它有一个名为“MODULES”的变量,它列出了我在构建中激活的所有模块。

这个列表由空格分隔,所以当我这样做时它看起来像这样echo $(MODULES)

module1 module2 module3 module4 mod5 mod6 mod7 module8 module9

我想做的是在编译时显示的某些列中显示此列表。

像这样:

Modules activated:
module1 module2 module3 
module4 mod5    mod6 
mod7    module8 module9

理想情况下,withs 列将调整为该列中最大模块的宽度(请参阅 参考资料mod7);列数会根据当前终端的宽度进行调整。

现在,我发现了一些似乎可以做到这一点的 unix 实用程序,例如column,但我无法使其与我的集合一起使用。

你有什么技巧可以让我这样做吗?

编辑

有了下面选择的答案,我终于在我的 Makefile 中破解了这个命令:

@printf '%s\n' $(MODULES) | sort | column
4

4 回答 4

5
printf '%-12s%-12s%s\n' $modules

这会在占位符出现在格式字符串中的次数内消耗变量的内容,并重复直到消耗所有内容。

column实用程序将自动为您生成列:

column <<< "$(printf '%s\n' $module)"

那是列优先。如果你想要行优先:

column -x <<< "$(printf '%s\n' $module)"
于 2012-05-21T14:21:15.213 回答
2

使用这个问题的答案,尝试这样的事情:

echo "Modules activated:"
for item in $modules; do 
    printf "%-8s\n" "${item}"
done | column

-x如果要转置输出,可能会添加到列命令。

就列数而言,这应该是终端敏感的。

于 2012-05-21T14:27:06.577 回答
1

column与和对齐fold

echo $modules | column -t | fold | column -t
于 2012-05-21T14:39:54.427 回答
0

You can use fold and tab, to get some rough formatting:

echo "module1 module2 module3 module4 mod5 mod6 mod7 module8 module9" | sed 's/ /\t/g' | fold -s -18module1 module2 
module3 module4 
mod5    mod6    
mod7    module8 
module9

but it will not work properly if some modulenames are longer than 8 characters, and some are shorter.

于 2012-05-21T14:35:00.170 回答