我使用column
命令来漂亮地打印表格,例如
column -s ':' -t < /etc/passwd
它有效,但所有分隔符都被空格替换,例如
_applepay * 260 260 applepay Account /var/db/applepay /usr/bin/false
_hidd * 261 261 HID Service User /var/db/hidd /usr/bin/false
我需要这样的东西
_applepay | * | 260 | 260 | applepay Account | /var/db/applepay | /usr/bin/false
有任何想法吗?
我刚看了说明书column
,没有这个选项
答案1
在查看 don_crissti 的答案并将其与 perl 表模块的各种输出格式进行比较时,我创建了这个可随时扩展的 perl 代码,以便它生成类似于非 BSD 代码列的输出:
#!/usr/bin/env perl
# @(#) p5 Demonstrate framework for non-bsd util-linux:column.
use strict;
use warnings;
use Text::FormatTable;
my ($input_separator) = ":";
my ($output_separator) = " | ";
my ( $rows, @a, @my_be, $back_end );
# Data rows from colon-separated data, e.g. passwd-format file.
while (<>) {
chomp;
@a = split /$input_separator/;
push @$rows, [@a];
}
# Prepare and print the table.
my $t = Text::FormatTable->new(
join( $output_separator, ('l') x @{ $rows->[0] } ) );
$t->head( @{ $rows->[0] } );
$t->row( @{ $rows->[$_] } ) for 1 .. @$rows - 1;
print $t->render;
exit(0);
当使用另一个答案中提到的 data2 文件运行时,会产生:
$ ./p5 data2
login | password | UID | GID | name | home | shell
daemon | x | 1 | 1 | daemon | /usr/sbin | /usr/sbin/nologin
bin | x | 2 | 2 | bin | /bin | /usr/sbin/nologin
_applepay | * | 260 | 260 | applepay | /var/db/applepay | /usr/bin/false
_hidd | * | 261 | 261 | HID Service | /var/db/hidd | /usr/bin/false
最美好的祝愿...干杯,drl
答案2
如果您使用column
fromutil-linux
您可以通过指定列分隔符
-o, --output-separator string
所以例如
column -s ':' -o ' | ' -t /etc/passwd
会打印类似的东西
root | x | 0 | 0 | root | /root | /bin/zsh
bin | x | 1 | 1 | bin | /bin | /usr/bin/nologin
daemon | x | 2 | 2 | daemon | / | /usr/bin/nologin
mail | x | 8 | 12 | mail | /var/spool/mail | /usr/bin/nologin
whatever | x | 14 | 11 | whatever | /srv/stuff | /usr/bin/nologin
答案3
用于sed
更换分离器。
sed 's/:/ | /g' /etc/passwd
答案4
对于这么长的帖子表示歉意。
有许多 Perl 模块可以执行表格式化。以下是其中一些的比较。数据位于文件 data2 中。脚本片段是:
# print-like-echo; print-line-with-visual-space.
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
FILE=${1-data2}
pl " Data file $FILE:"
head $FILE
pl " Results with column:"
column -s ':' -t $FILE
pl " Results from column | sed:"
column -s ':' -t < $FILE | sed -E 's/ ([^ ])/ | \1/g'
pl " Results of sed | column:"
sed 's/:/ | /g' $FILE | column -s ':' -t
./p4 $FILE
Perl 代码大约是文件 p4 上的 30 行 Perl(以及一些注释)。它将需要安装一些模块:
#!/usr/bin/env perl
# @(#) p4 Demonstrate various table formatters on password file.
use strict;
use warnings;
use Text::Table::Any;
my ( $rows, @a, @my_be, $back_end );
# Data rows from passwd-format file.
while (<>) {
chomp;
@a = split /:/;
push @$rows, [@a];
}
@my_be = ("Text::Table::Tiny");
# push @my_be, 'Text::Table::TinyColor';
# push @my_be, 'Text::Table::TinyColorWide';
# push @my_be, 'Text::Table::TinyWide';
push @my_be, 'Text::Table::Org';
push @my_be, 'Text::Table::CSV';
# push @my_be, 'Text::Table::HTML';
# push @my_be, 'Text::Table::HTML::DataTables';
push @my_be, 'Text::Table::Paragraph';
# push @my_be, 'Text::ANSITable';
push @my_be, 'Text::ASCIITable';
push @my_be, 'Text::FormatTable';
# push @my_be, 'Text::MarkdownTable';
push @my_be, 'Text::Table';
push @my_be, 'Text::TabularDisplay';
# push @my_be, 'Text::TestForError';
foreach $back_end (@my_be) {
print "\n\n----\n";
print " Backend table processor = $back_end\n";
print "\n";
print Text::Table::Any::table(
rows => $rows,
header_row => 1,
backend => $back_end,
);
}
运行./s1 data2的结果为:
-----
Data file data2:
login:password:UID:GID:name:home:shell
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
_applepay:*:260:260:applepay:/var/db/applepay:/usr/bin/false
_hidd:*:261:261:HID Service:/var/db/hidd:/usr/bin/false
-----
Results with column:
login password UID GID name home shell
daemon x 1 1 daemon /usr/sbin /usr/sbin/nologin
bin x 2 2 bin /bin /usr/sbin/nologin
_applepay * 260 260 applepay /var/db/applepay /usr/bin/false
_hidd * 261 261 HID Service /var/db/hidd /usr/bin/false
-----
Results from column | sed:
login | password | UID | GID | name | home | shell
daemon | x | 1 | 1 | daemon | /usr/sbin | /usr/sbin/nologin
bin | x | 2 | 2 | bin | /bin | /usr/sbin/nologin
_applepay | * | 260 | 260 | applepay | /var/db/applepay | /usr/bin/false
_hidd | * | 261 | 261 | HID | Service | /var/db/hidd | /usr/bin/false
-----
Results of sed | column:
login | password | UID | GID | name | home | shell
daemon | x | 1 | 1 | daemon | /usr/sbin | /usr/sbin/nologin
bin | x | 2 | 2 | bin | /bin | /usr/sbin/nologin
_applepay | * | 260 | 260 | applepay | /var/db/applepay | /usr/bin/false
_hidd | * | 261 | 261 | HID Service | /var/db/hidd | /usr/bin/false
----
Backend table processor = Text::Table::Tiny
+-----------+----------+-----+-----+-------------+------------------+-------------------+
| login | password | UID | GID | name | home | shell |
+-----------+----------+-----+-----+-------------+------------------+-------------------+
| daemon | x | 1 | 1 | daemon | /usr/sbin | /usr/sbin/nologin |
| bin | x | 2 | 2 | bin | /bin | /usr/sbin/nologin |
| _applepay | * | 260 | 260 | applepay | /var/db/applepay | /usr/bin/false |
| _hidd | * | 261 | 261 | HID Service | /var/db/hidd | /usr/bin/false |
+-----------+----------+-----+-----+-------------+------------------+-------------------+
----
Backend table processor = Text::Table::Org
| login | password | UID | GID | name | home | shell |
|-----------+----------+-----+-----+-------------+------------------+-------------------|
| daemon | x | 1 | 1 | daemon | /usr/sbin | /usr/sbin/nologin |
| bin | x | 2 | 2 | bin | /bin | /usr/sbin/nologin |
| _applepay | * | 260 | 260 | applepay | /var/db/applepay | /usr/bin/false |
| _hidd | * | 261 | 261 | HID Service | /var/db/hidd | /usr/bin/false |
----
Backend table processor = Text::Table::CSV
"login","password","UID","GID","name","home","shell"
"daemon","x","1","1","daemon","/usr/sbin","/usr/sbin/nologin"
"bin","x","2","2","bin","/bin","/usr/sbin/nologin"
"_applepay","*","260","260","applepay","/var/db/applepay","/usr/bin/false"
"_hidd","*","261","261","HID Service","/var/db/hidd","/usr/bin/false"
----
Backend table processor = Text::Table::Paragraph
login: daemon
password: x
UID: 1
GID: 1
name: daemon
home: /usr/sbin
shell: /usr/sbin/nologin
login: bin
password: x
UID: 2
GID: 2
name: bin
home: /bin
shell: /usr/sbin/nologin
login: _applepay
password: *
UID: 260
GID: 260
name: applepay
home: /var/db/applepay
shell: /usr/bin/false
login: _hidd
password: *
UID: 261
GID: 261
name: HID Service
home: /var/db/hidd
shell: /usr/bin/false
----
Backend table processor = Text::ASCIITable
.---------------------------------------------------------------------------------------.
| login | password | UID | GID | name | home | shell |
+-----------+----------+-----+-----+-------------+------------------+-------------------+
| daemon | x | 1 | 1 | daemon | /usr/sbin | /usr/sbin/nologin |
| bin | x | 2 | 2 | bin | /bin | /usr/sbin/nologin |
| _applepay | * | 260 | 260 | applepay | /var/db/applepay | /usr/bin/false |
| _hidd | * | 261 | 261 | HID Service | /var/db/hidd | /usr/bin/false |
'-----------+----------+-----+-----+-------------+------------------+-------------------'
----
Backend table processor = Text::FormatTable
login |password|UID|GID|name |home |shell
daemon |x |1 |1 |daemon |/usr/sbin |/usr/sbin/nologin
bin |x |2 |2 |bin |/bin |/usr/sbin/nologin
_applepay|* |260|260|applepay |/var/db/applepay|/usr/bin/false
_hidd |* |261|261|HID Service|/var/db/hidd |/usr/bin/false
----
Backend table processor = Text::Table
login password UID GID name home shell
daemon x 1 1 daemon /usr/sbin /usr/sbin/nologin
bin x 2 2 bin /bin /usr/sbin/nologin
_applepay * 260 260 applepay /var/db/applepay /usr/bin/false
_hidd * 261 261 HID Service /var/db/hidd /usr/bin/false
----
Backend table processor = Text::TabularDisplay
+-----------+----------+-----+-----+-------------+------------------+-------------------+
| login | password | UID | GID | name | home | shell |
+-----------+----------+-----+-----+-------------+------------------+-------------------+
| daemon | x | 1 | 1 | daemon | /usr/sbin | /usr/sbin/nologin |
| bin | x | 2 | 2 | bin | /bin | /usr/sbin/nologin |
| _applepay | * | 260 | 260 | applepay | /var/db/applepay | /usr/bin/false |
| _hidd | * | 261 | 261 | HID Service | /var/db/hidd | /usr/bin/false |
+-----------+----------+-----+-----+-------------+------------------+-------------------+
一个有趣的功能是模块 Text::ASCIITable 和 Text::Table 右对齐数字字段。
这是在这样的系统上:
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution : Debian 8.9 (jessie)
bash GNU bash 4.3.30
perl 5.20.2
最美好的祝愿...干杯,drl