Я бы сделал это в 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;
Пример использования:
$ ./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 |
+--------+--------------------------+-----------+--------------------------+
Если подумать, это можно сделать и без каких-либо неосновных модулей, благодаря 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
| | | | |
.
$ ./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 |
| | | | |
+--------+------------------------+-----------+-------------------------------+