如何在Bash中对齐空格分隔表的列?

Ric*_*ard 27 linux bash

我有一个文件,其中包含用空格分隔的任意数量的非对齐列.

我想对齐文件的列.

我看过col它看起来不合适的命令.

我可以写一个awk脚本,但似乎应该存在一个更明显的命令.

Mic*_*ski 48

您可能需要该column命令,通常--table / -t用于生成基本表格输出:

从手册页:

 -t, --table 
Run Code Online (Sandbox Code Playgroud)

确定输入包含的列数并创建表.默认情况下,列使用空格分隔,或使用--output-separator选项提供的字符分隔.表输出对于漂亮打印很有用.

column -t [file]

# or from stdin
cat file | column -t

# For a quick demonstration, format the output of mount
mount | column -t
Run Code Online (Sandbox Code Playgroud)

column有很多其他复杂的选择.man column详情.

  • 注意:如果您不想分割所有空格,请添加附加参数“-s”。例如,要在制表符上拆分 `column -t -s$'\t'` 请注意单引号和 `$` 的使用,两者都是将 `\t` 识别为制表符所必需的。 (4认同)