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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6602732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:58:01  来源:igfitidea点击:

How can I align the columns of a space separated table in Bash?

linuxbash

提问by Richard

I have a file with an arbitrary number of non-aligned columns separated with whitespace.

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

I would like to align the columns of the file.

我想对齐文件的列。

I've looked at the colcommand, and it doesn't seem appropriate.

我查看了col命令,它似乎不合适。

I could write an AWKscript, but it seems like a more obvious command should exist.

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

采纳答案by Michael Berkowski

You might want the columncommand, usually with --table / -tto produce basic tabular output:

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

From the man page:

从手册页:

 -t, --table 

Determine the number of columns the input contains and create a table. Columns are delimited with whitespace, by default, or with the charac‐ters supplied using the --output-separator option. Table output is useful for pretty-printing.

 -t, --table 

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

column -t [file]

# or from stdin
cat file | column -t

# For a quick demonstration, format the output of mount
mount | column -t

columnhas a lot of other complex options. man columnfor details.

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