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
How can I align the columns of a space separated table in Bash?
提问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 col
command, 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 column
command, usually with --table / -t
to 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
column
has a lot of other complex options. man column
for details.
column
有很多其他复杂的选择。man column
详情。