bash awk 在 linux 终端中输出类似表格或类似 excel 的列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12458729/
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-09-09 22:42:20  来源:igfitidea点击:

awk to output table-like or excel-like columns in linux terminal?

bashawkterminalmultiple-columns

提问by Ivan

I do a long pipe, that ends with ...| awk '{print $5"\t\t" $3"\t"$4}'in the Linux terminal. The columns are padded with tabs. The first column entries have different number of characters, so the second column results are not perfectly vertical. How to make the table look perfect?

我做了一个长管道,...| awk '{print $5"\t\t" $3"\t"$4}'以 Linux 终端结束。列用制表符填充。第一列条目具有不同数量的字符,因此第二列结果不是完全垂直的。如何让餐桌看起来完美?

enter image description here

在此处输入图片说明

回答by Kent

try to pipe the result to column -t:

尝试将结果通过管道传输到列 -t:

...| awk '{print "\t\t" "\t"}'|column -t

hope it helps

希望能帮助到你

回答by Luis Mu?oz

if your fields are tab separated the following one line script could print a table with cell borders

如果您的字段以制表符分隔,则以下一行脚本可以打印带有单元格边框的表格

sed -e 's/\t/_|/g' table.txt |  column -t -s '_' | awk '1;!(NR%1){print "-----------------------------------------------------------------------";}'

Description            |value                 |Comment
-----------------------------------------------------------------------
Internal               |322                   |
-----------------------------------------------------------------------
External               |42515                 |
-----------------------------------------------------------------------