UNIX:从带分隔符的文本文件创建表格格式的输出

Man*_*mar 3 unix shell awk

我需要从文本文件中获取表格格式输出,我正在通过以下 awk 命令实现它。

分隔文件

ACTIVE#1238917238971238#USA#The U.S. is a country of 50 states covering a vast swath of North America.
ACTIVE#21389721839781237812#INDIA#India, officially the Republic of India, is a country in South Asia.
ACTIVE#3121278372183782137812#AUSTRALIA#Australia, officially the Commonwealth of Australia, is a sovereign country comprising the mainland of the Australian continent
Run Code Online (Sandbox Code Playgroud)

AWK 命令

awk -F"#" 'BEGIN {{printf "%-80s\n","--------------------------------------------------------------------------------------------------------------------------------------"} {printf "|%-12s|%-30s|%-38s|%-50s|\n","STATUS","ID", "Country", "Description"} {printf "%-80s\n","--------------------------------------------------------------------------------------------------------------------------------------"}} {printf "|%-12s|%-30s|%-38s|%-50s|\n",$1,$2,$3,$4} END{printf "%-80s\n","--------------------------------------------------------------------------------------------------------------------------------------"}' /tmp/test.txt
Run Code Online (Sandbox Code Playgroud)

输出: 在此处输入图片说明

如果您可以看到 Description 列的输出,则它不会在其自己的列中格式化输出,而是由于字符串长度而弄乱了整个表。

有人可以查看并建议我如何更好地显示说明列的输出吗?

Sha*_*awn 5

我会在 perl 中使用Term::Table模块(可通过操作系统的包管理器安装或从 CPAN 安装),它将自动计算出列宽并根据需要换行:

#!/usr/bin/env perl
use strict;
use warnings;
use feature qw/say/;
use Term::Table;

my @lines = map { chomp; [ split /#/ ] } <>;
say for Term::Table->new(
    max_width => 80,
    header => ["Status", "ID", "Country", "Description"],
    rows => \@lines
    )->render;
Run Code Online (Sandbox Code Playgroud)

用法示例:

$ ./table.pl < input.txt
+--------+--------------------------+-----------+--------------------------+
| Status | ID                       | Country   | Description              |
+--------+--------------------------+-----------+--------------------------+
| ACTIVE | 1238917238971238         | USA       | The U.S. is a country of |
|        |                          |           |  50 states covering a va |
|        |                          |           | st swath of North Americ |
|        |                          |           | a.                       |
|        |                          |           |                          |
| ACTIVE | 21389721839781237812     | INDIA     | India, officially the Re |
|        |                          |           | public of India, is a co |
|        |                          |           | untry in South Asia.     |
|        |                          |           |                          |
| ACTIVE | 3121278372183782137812   | AUSTRALIA | Australia, officially th |
|        |                          |           | e Commonwealth of Austra |
|        |                          |           | lia, is a sovereign coun |
|        |                          |           | try comprising the mainl |
|        |                          |           | and of the Australian co |
|        |                          |           | ntinent                  |
+--------+--------------------------+-----------+--------------------------+
Run Code Online (Sandbox Code Playgroud)

想想看,它也可以在没有任何非核心模块的情况下完成,这要归功于 perl格式。我实际上更喜欢这种方式,因为它可以更好地自动换行(尽管更改表格的整体宽度甚至单个列的宽度会变得更加麻烦):

#!/usr/bin/env perl
use strict;
use warnings;
use feature qw/say/;

my ($status, $id, $country, $description);
while (<>) {
    chomp;
    ($status, $id, $country, $description) = split /#/;
    write;
}
say "+--------+------------------------+-----------+-------------------------------+";

format STDOUT_TOP =
+--------+------------------------+-----------+-------------------------------+
| Status | Id                     | Country   | Description                   |
+--------+------------------------+-----------+-------------------------------+
.

format STDOUT =
| @<<<<< | @<<<<<<<<<<<<<<<<<<<<< | @<<<<<<<< | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
  $status, $id,                     $country,   $description
|~~      |                        |           | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
                                                $description
|        |                        |           |                               |
.
Run Code Online (Sandbox Code Playgroud)
$ ./table.pl < input.txt
+--------+------------------------+-----------+-------------------------------+
| Status | Id                     | Country   | Description                   |
+--------+------------------------+-----------+-------------------------------+
| ACTIVE | 1238917238971238       | USA       | The U.S. is a country of 50   |
|        |                        |           | states covering a vast swath  |
|        |                        |           | of North America.             |
|        |                        |           |                               |
| ACTIVE | 21389721839781237812   | INDIA     | India, officially the         |
|        |                        |           | Republic of India, is a       |
|        |                        |           | country in South Asia.        |
|        |                        |           |                               |
| ACTIVE | 3121278372183782137812 | AUSTRALIA | Australia, officially the     |
|        |                        |           | Commonwealth of Australia, is |
|        |                        |           | a sovereign country           |
|        |                        |           | comprising the mainland of    |
|        |                        |           | the Australian continent      |
|        |                        |           |                               |
+--------+------------------------+-----------+-------------------------------+
Run Code Online (Sandbox Code Playgroud)