如何在整齐的列中格式化 bash 命令的输出

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

How can I format the output of a bash command in neat columns

bashawkzsh

提问by iconoclast

I have a function which outputs many rows of information which I want to format in columns. The problem is that the width of any particular "cell" (if I may use that term) of data is variable, so piping it to something like awk does not give me what I want.

我有一个函数,它输出我想在列中格式化的多行信息。问题是数据的任何特定“单元格”(如果我可以使用该术语)的宽度是可变的,因此将其传输到 awk 之类的东西并不能满足我的要求。

The function is "keys" (not that it matters) and I'm trying something like this:

该功能是“键”(并不重要),我正在尝试这样的事情:

$ keys | awk '{ print "\t\t"  }'

but the output (a snippet of it, that is) looks like this:

但输出(它的一个片段,即)看起来像这样:

"option-y"      yank-pop
"option-z"      execute-last-named-cmd
"option-|"      vi-goto-column
"option-~"      _bash_complete-word
"option-control-?"      backward-kill-word
"control-_"     undo
"control-?"     backward-delete-char

How can I force things to stay in neat columns? Is this possible with awk, or do I need to use something else?

我怎样才能强迫事物保持整齐的列?awk 可以做到这一点,还是我需要使用其他东西?

回答by Ignacio Vazquez-Abrams

column(1)is your friend.

column(1)是你的朋友。

$ column -t <<< '"option-y"      yank-pop
> "option-z"      execute-last-named-cmd
> "option-|"      vi-goto-column
> "option-~"      _bash_complete-word
> "option-control-?"      backward-kill-word
> "control-_"     undo
> "control-?"     backward-delete-char
> '
"option-y"          yank-pop
"option-z"          execute-last-named-cmd
"option-|"          vi-goto-column
"option-~"          _bash_complete-word
"option-control-?"  backward-kill-word
"control-_"         undo
"control-?"         backward-delete-char

回答by Koterpillar

Found this by searching for "linux output formatted columns".

通过搜索“linux output formatted columns”找到了这个。

http://www.unix.com/shell-programming-scripting/117543-formatting-output-columns.html

http://www.unix.com/shell-programming-scripting/117543-formatting-output-columns.html

For your needs, it's like:

根据您的需求,它就像:

awk '{ printf "%-20s %-40s\n", , }'

回答by geekosaur

While awk's printfcan be used, you may want to look into pror (on BSDish systems) rsfor formatting.

虽然可以使用awk's printf,但您可能需要查看pr或 (在 BSDish 系统上)rs进行格式化。

回答by user2961066

Since AIX doesn't have a "column" command, I created the simplistic script below. It would be even shorter without the doc & input edits... :)

由于 AIX 没有“列”命令,我创建了下面的简单脚本。如果没有文档和输入编辑,它会更短...... :)

#!/usr/bin/perl
#       column.pl: convert STDIN to multiple columns on STDOUT
#       Usage: column.pl column-width number-of-columns  file...
#
$width = shift;
($width ne '') or die "must give column-width and number-of-columns\n";
$columns = shift;
($columns ne '') or die "must give number-of-columns\n";
($x = $width) =~ s/[^0-9]//g;
($x eq $width) or die "invalid column-width: $width\n";
($x = $columns) =~ s/[^0-9]//g;
($x eq $columns) or die "invalid number-of-columns: $columns\n";

$w = $width * -1; $c = $columns;
while (<>) {
        chomp;
        if ( $c-- > 1 ) {
                printf "%${w}s", $_;
                next;
        }
        $c = $columns;
        printf "%${w}s\n", $_;
}
print "\n";

回答by Vytenis Bivainis

Try

尝试

xargs -n2  printf "%-20s%s\n"

or even

甚至

xargs printf "%-20s%s\n"

if input is not very large.

如果输入不是很大。

回答by Lewis R

If your output is delimited by tabs a quick solution would be to use the tabscommand to adjust the size of your tabs.

如果您的输出由制表符分隔,一个快速的解决方案是使用该tabs命令来调整制表符的大小。

tabs 20
keys | awk '{ print "\t\t"  }'